date-and-time-management.jpg
PHP Date and Time Timezone

PHP Date and Time

What is timestamp?

A timestamp is a sequence of characters or encoded information identifying when a certain event occurred, usually giving date and time of day, sometimes accurate to a small fraction of a second. The term derives from rubber stamps used in offices to stamp the current date, and sometimes time, in ink on paper documents, to record when the document was received. Common examples of this type of timestamp are a postmark on a letter or the “in” and “out” times on a time card.

What is Timezone?

A time zone is a region of the globe that observes a uniform standard time for legal, commercial and social purposes. Time zones tend to follow the boundaries of countries and their subdivisions instead of longitude, because it is convenient for areas in close commercial or other communication to keep the same time.

What is UTC?

Coordinated Universal Time or UTC is the primary time standard by which the world regulates clocks and time. It is within about 1 second of mean solar time at 0° longitude and is not adjusted for daylight saving time. It is effectively a successor to Greenwich Mean Time (GMT).

What is GMT?

Greenwich Mean Time (GMT) is the mean solar time at the Royal Observatory in Greenwich, London, reckoned from midnight. At different times in the past, it has been calculated in different ways, including being calculated from noon; as a consequence, it cannot be used to specify a precise time unless a context is given.

What is IST?

Indian Standard Time (IST) is the time zone observed throughout India, with a time offset of UTC+05:30. India does not observe daylight saving time or other seasonal adjustments. In military and aviation time IST is designated E* (“Echo-Star”).

PHP Date and Time Functions

PHP has several date and time functions which can be used to get the current date and time. Here are some examples:

In PHP, we can use the date function and the DateTime class to get the server’s date, time, timestamp, and timezone.

1. Server Date and Time

date_default_timezone_set('Asia/Kolkata'); // To set your timezone
$date_time = date("d-m-Y (D) H:i:s"); // To format the date and time
echo "Current date and local time on this server is $date_time";

Output:

Current date and local time on this server is 10-01-2024 (Wed) 10:11:12

In the above example, we have used the date_default_timezone_set function to set the timezone to 'Asia/Kolkata'. You can replace 'Asia/Kolkata' with your actual timezone. You can find a list of supported timezones here.

2. Timestamp

$timestamp = time(); // To get the current Unix timestamp
echo "Current Unix timestamp is $timestamp";

Output:

Current Unix timestamp is 1704861672

In the above example, we have used the time function to get the current Unix timestamp. You can read more about Unix timestamp here.

3. Timezone

$tz = 'Europe/London'; // To set any timezone
$dt = new DateTime("now", new DateTimeZone($tz)); // Here create a DateTime object
print_r($dt);
echo '<br>';
echo $dt->format('Y-m-d H:i:s'); // To output the date and time
echo '<br>';
echo $dt->format('e'); // To output the timezone

Output:

DateTime Object ( [date] => 2024-01-10 10:11:12.000000 [timezone_type] => 3 [timezone] => Europe/London )
2024-01-10 10:11:12
Europe/London

In the above example, we have used the DateTime class to get the timezone. You can replace 'Europe/London' with your actual timezone. You can find a list of supported timezones here.

Remember to replace 'Asia/Kolkata' and 'Europe/London' with your actual timezone.

Questions?

  • How to get current time in specific timezone using PHP?
  • PHP to get date with timezone?
  • How do I get the current date and time in PHP?
  • How to get date and time from server?
Was this helpful?
How was this post?
Subscribe to our newsletter
Stay updated on new releases and features, guides, and case studies. Get tips, technical guides, and best practices. Once a month. Right in your inbox.