Converting a Timestamp to Date and Time

In this tutorial, you learn how to get a local date and time with getdate() function, convert a timestamp into date and time with getdate() and date() functions, and convert a date and time into a timestamp with strtotime and mktime functions.

  1. The getdate() function
  2. Converting timestamp to date and time
  3. Converting date and time to timestamp


The getdate() function takes one optional parameter: $timestamp. If a timestamp is provided the function returns an associative array containing date and time values for that timestamp, otherwise it returns date and time values for the local time.

 getdate(?int $timestamp = null): array

The following table lists the elements contained in the array returned by getdate().

KeyPossible Value
seconds0 to 59
minutes0 to 59
hours0 to 23
mday1 to 31 (Day of the month)
wday0 to 6 (Day of the week)
0 for Sunday, 1 for Monday, and so on.
mon1 to 12 (Month of the year)
yearYear, 4 digits. For example 2022
yday0 to 365 (Day of the year)
weekdaySunday to Saturday
monthJanuary to December

Example: Get the current local date and time

  $array = getdate();
 /* Prints: 
  [seconds] => 54
  [minutes] => 52
  [hours] => 10
  [mday] => 6
  [wday] => 6
  [mon] => 8
  [year] => 2022
  [yday] => 217
  [weekday] => Saturday
  [month] => August
  [0] => 1659775974 ) */

Converting a Timestamp to Date or Time

If you have a timestamp and you want to convert it to a readable date/time form, use the getdate() function, see the following example:

 $a = getdate(2009077887);
 echo $a['weekday'] .' '.
      $a['mday']    .' '.
      $a['month']   .' '.
      $a['year']    .', '.
      $a['hours']   .':'.
      $a['minutes'] .':'.
 # Prints: Wednesday 31 August 2033, 7:11:27

The getdate() function returns an associative array, use date() function if you want a string. The date() function not only parses a timestamp it also returns a formatted date and time string as per your requirements. The following example demonstrates how the date() function format and returns the string:

 echo date('l d F Y, g:i:s', 2009077887);
 # Prints: Wednesday 31 August 2033, 7:11:27

Visit Formatting Date and Time with date() tutorial to read more about the date() function.

Converting date or time into a timestamp

There are various ways to generate a Unix timestamp by using the time(), mktime(), or strtotime() functions, see the following example:

 echo mktime(7,11,27,8,31,2033) . '<br>';
 # Prints: 2009077887
 echo strtotime('31 August 2033, 7:11:27');
 # Prints: 2009077887

For more information, visit How to convert or generate timestamps tutorial.

The Date and Time Tutorials: