Date/time session variables in PHP

 

In PHP, date and time functions are used to retrieve or format temporal data, while session variables are used to store that data so it persists as a user moves from page to page.

 

1. PHP Date and Time Functions
These functions allow you to get the current time from your server and format it for users.
  • time(): Returns the current time as a Unix timestamp (the number of seconds since January 1, 1970).
  • date(format, [timestamp]): Formats a timestamp into a readable string.
    • d/m/Y
       

       Day/Month/Year (e.g., 19/04/2026).

    • H:i:s
       

       Hours:Minutes:Seconds (24-hour format).

  • date_default_timezone_set("Zone"): Sets the time zone for all date/time functions in that script (e.g., "Asia/Kolkata").

 

2. Session Variables for Time Tracking

Since standard variables are “forgotten” as soon as a script finished running, use the $_SESSION super global to store time data for tasks like tracking login times or session timeouts.