PHP Date & Time Functions

    PHP supports some date and time functions that allow you to get the date and time details from the server where the script is running. Below are some date and time constants that you can use with the functions throughout the script execution.

    PHP Date and Time constants

    1. "DATE_ATOM

    Atom (example: 2005-08-15T16:13:03+0000)"

    2. "DATE_COOKIE

    HTTP Cookies (example: Sun, 14 Aug 2005 16:13:03 UTC)"

    3. "DATE_ISO8601

    ISO-8601 (example: 2005-08-14T16:13:03+0000)"

    4. "DATE_RFC822

    RFC 822 (example: Sun, 14 Aug 2005 16:13:03 UTC)"

    5. "DATE_RFC850

    RFC 850 (example: Sunday, 14-Aug-05 16:13:03 UTC)"

    6. "DATE_RFC1036

    RFC 1036 (example: Sunday, 14-Aug-05 16:13:03 UTC)"

    7. "DATE_RFC1123RFC

    RFC 1123 (example: Sun, 14 Aug 2005 16:13:03 UTC)"

    8. "DATE_RFC2822

    RFC 2822 (Sun, 14 Aug 2005 16:13:03 +0000)"

    9. "DATE_RSS

    RSS (Sun, 14 Aug 2005 16:13:03 UTC)"

    10. "DATE_W3C

    World Wide Web Consortium (example: 2005-08-14T16:13:03+0000)"

    11. "SUNFUNCS_RET_TIMESTAMP

    Timestamp ( Available in 5.1.2 )"

    12. "SUNFUNCS_RET_STRING

    Hours:minutes (example: 08:02) ( Available in 5.1.2 )"

    13. "SUNFUNCS_RET_DOUBLE

    Hours as a floating-point number (example 8.75)( Available in 5.1.2 )"

    PHP Date and Time functions

    1. check_date()

    This function will take three parameters- month, day and the year and checks if the given date belongs to a gregorian date or not.

    Syntax

    checkdate ( int $month , int $day , int $year )

    Example

    <?php
       var_dump(checkdate(11, 07, 1989));
    ?>

    Output

    bool(true)

    2. date_create()

    This function is the constructor of the DateTime class which specifies the date and time and accept the time string and time zone. This function will create an object for the current date/time and return the object.

    Syntax

    date_create([$date_time, $timezone]);

    Example

    <?php
     $date_time_Obj = date_create("25-09-1989");
     $format = date_format($date_time_Obj, "d-m-Y H:i:s");
     print($format);
    ?>

    Output

    25-09-1989 00:00:00

    3. date_date_set()

    This function will allow you to reset the date-time object and takes four mandatory parameters.

    Syntax

    date_date_set($object, $year, $month, $day)

    Example

    <?php
        $date = new DateTime();
        date_date_set($date, 2019, 07, 17);
        print("Date: ".date_format($date, "Y/m/d"));
    ?>

    Output

    Date: 2019/07/17

    4. date_default_timezone_get()

    This function will work without passing any parameters and returns the default timezone that is used by all the functions defined within any script. If you haven’t set any timezone before then this function will provide you with the default one.

    Syntax

    date_default_timezone_get()

    Example

    <?php    
       $timeZone = date_default_timezone_get();
       print("Default timezone: ".$timeZone);
    ?>

    Output

    Default timezone: UTC

    5. date_default_timezone_set()

    This function will allow you to set the timezone for all the functions defined within the script.

    Syntax

    date_default_timezone_set(timezone)

    Example

    <?php    
       $tz = 'Indian/Mahe';   
       date_default_timezone_set($tz);
       $timeZone = date_default_timezone_get();
       print("Default timezone: ".$timeZone);
       ?>

    Output

    Default timezone: Indian/Mahe

    6. date_format()

    This function will allow you to pass two parameters- date-time object and the string defining the format of the date.

    Syntax

    date_format($date_time_object, $format)

    Example

    <?php    
       $date_time_Obj = date_create("25-09-1989");
       $format = date_format($date_time_Obj, "y-d-m");
       print("Date in yy-dd-mm format: ".$format); 
       ?>

    Output

    Date in yy-dd-mm format: 89-25-09

    7. date_modify()

    This function will allow you to modify the date within the DateTime object. This function will take two parameters- the date-time object and the second parameter specifies the modification string.

    Syntax

    date_modify($object, $modify)

    Example

    <?php    
       //$date_time_Obj = date_create("25-09-1989");
       $date = date_modify(new DateTime(), "+15 day");   
       print("Date: ".date_format($date, "Y/m/d"));
       ?>

    Output

    Date: 2020/08/13

    8. date_isodate_set()

    This function will allow you to set the ISO date and take four parameters- the date-time object, the specific year, the week of that month and the day of the week.

    Syntax

    date_isodate_set($object, $year, $week, $day)

    Example

    <?php    
          //$date_time_Obj = date_create("25-09-1989");
          $date = date_modify(new DateTime(), "+15 day");  
          date_isodate_set($date, 2019, 03, 3);   
          print("Date: ".date_format($date, "Y/m/d"));
       ?>

    Output-

    Date: 2019/01/16

    9. date_offset_get()

    This function takes up one parameter that is the object of the date-time and provides you with the timezone offset of that given date.

    Syntax

    date_offset_get($object)

    Example

    <?php    
          //$date_time_Obj = date_create("25-09-1989");
          $date = date_modify(new DateTime(), "+15 day");  
          $offset = date_offset_get( $date );
          print("Offset: ".$offset);
       ?>

    Output

    Offset: 0

    10. date_parse()

    This function will take a single mandatory parameter that is the date, this function will parse the given date and return an array containing the information about the given date.

    Syntax

    date_parse($date)

    Example

    <?php    
          //$date_time_Obj = date_create("25-09-1989");
          //$date = date_modify(new DateTime(), "+15 day");  
          print_r(date_parse("2009-11-09 07:30:25.5")); 
       ?>

    Output

    Array ( [year] => 2009 [month] => 11 [day] => 9 [hour] => 7 [minute] => 30 [second] => 25 [fraction] => 0.5 [warning_count] => 0 [warnings] => Array ( ) [error_count] => 0 [errors] => Array ( ) [is_localtime] => )

    11. date_sun_info()

    This function will allow you to find information about the sunrise/sunset and, begin/end of the twilight that is all based on the three mandatory passed parameters- timestamp, latitude and the longitude information.

    Syntax

    date_sun_info($timestamp, $latitude, $longitude)

    Example

    <?php    
          //$date_time_Obj = date_create("25-09-1989");
          //$date = date_modify(new DateTime(), "+15 day"); 
          $sun_info = date_sun_info("02-17-2012", 20.5937, 78.9629);
          print_r($sun_info);
       ?>
       ?>

    12. date_sunrise()

    This function will provide you with the sunrise time of the particular day for the given timestamp. This function will take only one mandatory parameter that is the timestamp else are optional.

    Syntax

    date_sunrise($timestamp, [$format, $latitude, $longitude, $zenith, $gmtoffset])

    13. date_sunset()

    This function returns the sunset time for the particular day for the given timestamp. This works exactly the same as the date_sunrise() function.

    Syntax

    date_sunset($timestamp, [$format, $latitude, $longitude, $zenith, $gmtoffset])

    14. date_time_set()

    This function will allow you to reset the time of the date-time object. This function will take the five parameters, among which three are mandatory and the last two parameters are optional- object, hours, minute, second and microseconds.

    Syntax

    date_time_set($object, $hours, $minutes, $seconds, $microseconds)

    Example

    <?php    
          //$date_time_Obj = date_create("25-09-1989");
          //$date = date_modify(new DateTime(), "+15 day");  
       print("\n");
       $date = new DateTime();
       //Setting the date
       date_time_set($date, 7, 20, 35);   
       print("Date: ".date_format($date, "Y/m/d H:i:s"));
       ?>
       ?>

    Output

    Date: 2020/07/29 07:20:35 ?>

    15. date_timezone_get()

    This function will allow you to get the timezone object relative to the given date-time object. This function will take one mandatory parameter that is a date-time object.

    Syntax

    date_timezone_get($object)

    Example

    <?php    
          //$date_time_Obj = date_create("25-09-1989");
          //$date = date_modify(new DateTime(), "+15 day");  
       $date = date_create("25-09-1989");   
       $res = date_timezone_get($date);
       $timeZone_name = timezone_name_get($res);
       print("Timezone: ".$timeZone_name);
       ?>
       ?>
    

    Output

    Timezone: UTC ?>

    16. date_timezone_set()

    This function will allow you to set the specified timezone according to the given date-time. This function will take two mandatory parameters- the date-time object and the timezone.

    Syntax

    date_timezone_set($object, $timezone)

    Example

    <?php    
       //$date_time_Obj = date_create("25-09-1989");
       $date = date_modify(new DateTime(), "+15 day");  
       $tz = new DateTimeZone('Indian/Mahe');   
       $res = date_timezone_set($date, $tz);   
       print("Timezone: ".timezone_name_get(date_timezone_get($date)) );
       ?>

    Output

    Timezone: Indian/Mahe

    17. date()

    This function will allow you to provide with the formatted date according to the given format. This function takes up two parameters- the format string and the timestamp to be changed.

    Syntax

    date($format, $timestamp)

    Example

    <?php    
          //$date_time_Obj = date_create("25-09-1989");
          //$date = date_modify(new DateTime(), "+15 day");  
       print("\n");  
       $date = date("D M d Y");
       print("Date: ".$date);
       ?>

    Output

    Date: Wed Jul 29 2020

    18. getdate()

    This function will provide you with information about the given date in the form of the array. This will take a single parameter which is optional if not specifies the function will work on the current timestamp.

    Syntax

    getdate([$timestamp])

    Example

    <?php      
       //$date_time_Obj = date_create("25-09-1989");
       //$date = date_modify(new DateTime(), "+15 day");  
       $info = getdate();
       print_r($info); 
       ?>

    Output

    Array ( [seconds] => 57 [minutes] => 31 [hours] => 2 [mday] => 29 [wday] => 3 [mon] => 7 [year] => 2020 [yday] => 210 [weekday] => Wednesday [month] => July [0] => 1595989917 )

    19. gettimeofday()

    This function will return the array format current time details of the given day. In case if you pass the boolean value (true) then the function will return the time in the floating-point number. This function will take only a single parameter which is optional which specifies if you want the result as floating-point or not.

    Syntax

    gettimeofday($return_float)

    Example

    <?php    
     //$date_time_Obj = date_create("25-09-1989");
     //$date = date_modify(new DateTime(), "+15 day");  
     $time = gettimeofday();    
     print_r($time);
     ?>

    Output

    Array ( [sec] => 1595989954 [usec] => 799691 [minuteswest] => 0 [dsttime] => 0 )

    20. gmdate()

    This function will format the given date as per the given format string.

    Syntax

    gmdate($format, $timestamp)

    Example

    <?php    
        //$date_time_Obj = date_create("25-09-1989");
        //$date = date_modify(new DateTime(), "+15 day");  
        $date = gmdate("D M d Y");
        print("Date: ".$date);
       ?>

    Output

    Date: Wed Jul 29 2020

    21. gmmktime()

    This function will take up the seven parameters among which first five parameters are mandatory and the last two are optional- hour, minute, second, month, day, year and is_dst. This function will return the Unix timestamp for the specified GMT date, if there is no passed parameter then it will use the current timestamp. Syntax

    gmmktime($hour, $minute, $second, $month, $day,$ year, $is_dst)

    Example

    <?php     
       //$date_time_Obj = date_create("25-09-1989");
       //$date = date_modify(new DateTime(), "+15 day");  
       $timestamp = gmmktime();   
       print($timestamp);
       print("\n");
       ?>

    Output

    1595990025

    22. gmstrftime()

    This function will allow you to format the GMT/UTC time/date as per the given format string based on the locale settings.

    Syntax

    gmstrftime($format, $timestamp)

    Example

    <?php    
       //$date_time_Obj = date_create("25-09-1989");
       //$date = date_modify(new DateTime(), "+15 day");  
       $date = gmstrftime("%A %d %B %G");
       $time = gmstrftime("%T");
       print("Date: ".$date ."\n");
       print("Time: ".$time);
       ?>

    Output

    Date: Wednesday 29 July 2020 Time: 04:03:16

    23. idate()

    This function will allow you to format the local date/time in the specified given format which is passed as the parameter to the function. The second parameter is optional and will consider the current timestamp if there is no value passed for it.

    Syntax

    idate($format, [$timestamp])

    Example

    <?php    
       //$date_time_Obj = date_create("25-09-1989");
       //$date = date_modify(new DateTime(), "+15 day");  
       $format = "U";
       $res = idate($format);
       print("Timestamp: ".$res);
       ?>

    Output

    Timestamp: 1595995442

    24. microtime()

    This function will allow you to get the Unix timestamp in microseconds and the result is returned as a string value containing microseconds that are separated from the seconds with space. The parameter that is passed is optional.

    Syntax

    microtime($get_as_float)

    Example

    <?php    
          //$date_time_Obj = date_create("25-09-1989");
          //$date = date_modify(new DateTime(), "+15 day");  
          $time = microtime();
          print($time);
       ?>

    Output

    0.79335700 1595995478

    25. mktime()

    This function will allow you to get the Unix timestamp for the given date. This function will take parameters- hour, minute, second, month, day, year and is_dst among which all parameters are mandatory except the second parameter.

    Syntax

    mktime($hour, $minute, $second, $month, $day,$ year, $is_dst)

    Example

    <?php    
       //$date_time_Obj = date_create("25-09-1989");
       //$date = date_modify(new DateTime(), "+15 day");  
       $timestamp = mktime();   
       print($timestamp);
       ?>

    Output

    1595995510

    26. strftime()

    This function will allow you to format the locale date/time as per the provided string format. The first parameter is mandatory and the second parameter is optional, if not provided then the function will consider the current timestamp.

    Syntax

    strftime($format [, $timestamp])

    Example

    <?php    
       //$date_time_Obj = date_create("25-09-1989");
       //$date = date_modify(new DateTime(), "+15 day");  
       $date = strftime("%A %d %B %G");
       $time = strftime("%T");
       print("Date: ".$date ."\n");
       print("Time: ".$time);
       ?>

    Output

    Date: Wednesday 29 July 2020 Time: 04:05:43

    27. strptime()

    This function will allow you to format the date string as per the provided date string. This function will accept two parameters and both are mandatory.

    Syntax

    strptime($date, $format)

    Example

    <?php    
       //$date_time_Obj = date_create("25-09-1989");
       //$date = date_modify(new DateTime(), "+15 day");  
       $format = '%A %d %B %G %T';
       $strf = strftime($format);
       $res = strptime($strf, $format);
       print_r($res);
       ?>

    Output

    Array ( [tm_sec] => 15 [tm_min] => 6 [tm_hour] => 4 [tm_mday] => 29 [tm_mon] => 6 [tm_year] => 0 [tm_wday] => 3 [tm_yday] => 209 [unparsed] => )

    28. strtotime()

    This function will allow you to parse the given date/time string as the Unix timestamp.

    Syntax

    strtotime($time)

    Example

    <?php    
       //$date_time_Obj = date_create("25-09-1989");
       //$date = date_modify(new DateTime(), "+15 day");  
       $str = strtotime("12 September 2009");
       print("Timestamp: ".$str); 
       ?>

    Outout

    Timestamp: 1252713600

    29. time()

    This function will provide you with the number of seconds between the Epoch and the current time. This function will take no parameters.

    Syntax

    time(void)

    Example

    <?php    
          //$date_time_Obj = date_create("25-09-1989");
          //$date = date_modify(new DateTime(), "+15 day");  
          $time = time();
          print("Current Timestamp: ".$time);
       ?>

    Output

    Current Timestamp: 1595995653

    30. timezone_abbrebriations_list()

    This function will provide you details like- dst, offset and the name of the timezone in the form of an array. This function will not accept any parameters.

    Syntax

    timezone_abbreviations_list()

    Example

    <?php    
      //$date_time_Obj = date_create("25-09-1989");
      //$date = date_modify(new DateTime(), "+15 day");  
      $res = timezone_abbreviations_list();
      print_r($res["acst"]);
      ?>

    Output

    Array ( [0] => Array ( [dst] => [offset] => 34200 [timezone_id] => Australia/Adelaide ) [1] => Array ( [dst] => [offset] => 34200 [timezone_id] => Australia/Broken_Hill ) [2] => Array ( [dst] => [offset] => 34200 [timezone_id] => Australia/Darwin ) [3] => Array ( [dst] => [offset] => 34200 [timezone_id] => Australia/North ) [4] => Array ( [dst] => [offset] => 34200 [timezone_id] => Australia/South ) [5] => Array ( [dst] => [offset] => 34200 [timezone_id] => Australia/Yancowinna ) )

    31. timezone_identifiers_list()

    This function will provide you with all the identifiers in the php in the array form. This function will take two parameters and both are optional.

    Syntax

    timezone_identifiers_list([$what, $country])

    32. timezone_name_from_abbr()

    This function will provide you with the timezone name with the help of the provided abbreviation.

    Syntax

    timezone_name_from_abbr($abbr, [$gmtoffset[, $isdst]]);

    Example

    <?php    
      //$date_time_Obj = date_create("25-09-1989");
      //$date = date_modify(new DateTime(), "+15 day");  
      $res = timezone_name_from_abbr("PST");   
      print($res);
       ?>

    Output

    America/Los_Angeles

    Where abbr specifies the abbreviation from which you want to know the timestamp, gmtOffset specifies the offset from the GMT in seconds and isdst specifies the daylight saving of that timezone.

    33. timezone_name_get()

    This function will allow you to pass the date/time object to it as a parameter and it will return the time zone corresponding to it. This function will take one parameter and is mandatory.

    Syntax

    timezone_name_get($object)

    Example

    <?php    
       //$date_time_Obj = date_create("25-09-1989");
       //$date = date_modify(new DateTime(), "+15 day");  
       $tz = new DateTimeZone('Indian/Mahe');   
       $res = timezone_name_get($tz);
       print("Timezone: ".$res);
       ?>

    Output

    Timezone: Indian/Mahe

    34. timezone_offset_get()

    This function will allow you to provide timezone and the date-time values as the parameter and both are mandatory to provide you with the timezone offset from the GMT.

    Syntax

    timezone_offset_get($object, $datetime)

    Example

    <?php     
       //$date_time_Obj = date_create("25-09-1989");
       //$date = date_modify(new DateTime(), "+15 day");  
       $tz = new DateTimeZone("Indian/mahe");
       $datetime = date_create("now", new DateTimeZone("Asia/Taipei"));
       $res = timezone_offset_get($tz, $datetime );
       print($res);
       ?>

    Output

    14400

    35. timezone_open()

    This function will allow you to create the DateTimeZone object on the basis of the provided timezone string as a parameter.

    Syntax

    timezone_open($timezone)

    Example

    <?php    
       //$date_time_Obj = date_create("25-09-1989");
       //$date = date_modify(new DateTime(), "+15 day");  
       $tz = "Indian/mahe";
       $res = timezone_open($tz);   
       print_r($res);
       ?>

    Output

    DateTimeZone Object ( [timezone_type] => 3 [timezone] => Indian/mahe )

    36. timezone_transitions_get()

    This function will allow you to provide the transition for the given timezone. This function will accept three parameters among which first parameter is mandatory and the last two parameters are optional. The first parameter specifies the DateTimeZone object.

    Syntax

    timezone_transitions_get($object, $timestamp_start, $timestamp_end)

    Example

    <?php    
       //$date_time_Obj = date_create("25-09-1989");
       //$date = date_modify(new DateTime(), "+15 day");  
       $tz = new DateTimeZone("Indian/Mahe");
       $list = timezone_transitions_get($tz);
       print_r($list);
       ?>

    Output

    Array ( [0] => Array ( [ts] => -9223372036854775808 [time] => -292277022657-01-27T08:29:52+0000 [offset] => 13308 [isdst] => [abbr] => LMT ) [1] => Array ( [ts] => -576460752303423488 [time] => -18267312070-10-26T17:01:52+0000 [offset] => 13308 [isdst] => [abbr] => LMT ) [2] => Array ( [ts] => -2006653308 [time] => 1906-05-31T20:18:12+0000 [offset] => 14400 [isdst] => [abbr] => +04 ) [3] => Array ( [ts] => 2147483647 [time] => 2038-01-19T03:14:07+0000 [offset] => 14400 [isdst] => [abbr] => +04 ) )

    37. date_add()

    This function will take a DateTime object and DateInterval object as a parameter and add the interval to the specified date object.

    Syntax

    date_add($object, $interval)

    Example

    <?php    
       //$date_time_Obj = date_create("25-09-1989");
       //$date = date_modify(new DateTime(), "+15 day");  
       $date = date_create("25-09-1989");
       //Adding interval to the date
       $res = date_add($date, new DateInterval('PT10H30S'));   
       //formatting the date to print it
       $format = date_format( $res, "d-m-Y H:i:s");
       print($format);
       ?>

    Output

    25-09-1989 10:00:30

    38. date_create_from_format()

    This function will be given three parameters- format string and the time string both are mandatory and the third is the timezone which is optional. This function will parse the given time string in the given format and return the DateTime object.

    Syntax

    date_create_from_format($date)

    Example

    <?php    
       //$date_time_Obj = date_create("25-09-1989");
       //$date = date_modify(new DateTime(), "+15 day");  
       $date = "25-Mar-1989";
       $format = "d-M-Y";
       $res = date_create_from_format($format, $date);
       print(date_format($res, "Y-m-d"));
       ?>

    Output

    1989-03-25

    39. date_diff()

    This function will provide you with the difference between the given two DateTime objects. This function will take three parameters among which the first two parameters are mandatory which are the two DateTime objects and the third parameter is the absolute which is optional and specifies the difference must be positive. Syntax

    date_diff($datetime1, $datetime2[, $absolute])

    Example

    <?php    
       //$date_time_Obj = date_create("25-09-1989");
       //$date = date_modify(new DateTime(), "+15 day");  
       $date1 = date_create("25-09-1989");
       $date2 = date_create("1-09-2012");
       $interval = date_diff($date1, $date2);
       print($interval->format('%Y years %d days'));
       ?>

    Output

    22 years 7 days

    40. date_parse_from_format()

    This function will accept two parameters and are mandatory which are format string and the date string. This function will provide you with the details about the date in the specified format as passed to the function.

    Syntax

    date_parse($format, $date)

    Example

    <?php    
       //$date_time_Obj = date_create("25-09-1989");
       //$date = date_modify(new DateTime(), "+15 day");  
       $date = "25-Mar-1989";
       $format = "d-M-Y";
       $res = date_parse_from_format($format, $date);
       print_r($res);
       ?>

    Output

    Array ( [year] => 1989 [month] => 3 [day] => 25 [hour] => [minute] => [second] => [fraction] => [warning_count] => 0 [warnings] => Array ( ) [error_count] => 0 [errors] => Array ( ) [is_localtime] => )

    41. date_parse()

    This function will parse the given date to the function and provide the details about the date in the array format. This function will take a single parameter which is mandatory- date.

    Syntax

    date_parse($date)

    Example

    <?php    
          //$date_time_Obj = date_create("25-09-1989");
          //$date = date_modify(new DateTime(), "+15 day");  
          print_r(date_parse("2009-11-09 07:30:25.5")); 
       ?>

    Output

    Array ( [year] => 2009 [month] => 11 [day] => 9 [hour] => 7 [minute] => 30 [second] => 25 [fraction] => 0.5 [warning_count] => 0 [warnings] => Array ( ) [error_count] => 0 [errors] => Array ( ) [is_localtime] => )

    42. date_sub()

    This function will take up two mandatory parameters- The DateTime object and the DateInterval object. This function will then provide you with the difference between both the dates.

    Syntax

    date_sub($object, $interval)

    Example

    <?php    
       //$date_time_Obj = date_create("25-09-1989");
       //$date = date_modify(new DateTime(), "+15 day");  
       $date = date_create("25-09-2019");
       //Adding interval to the date
       $res = date_sub($date, new DateInterval('PT10H30S'));   
       //formatting the date to print it
       $format = date_format( $res, "d-m-Y H:i:s");
       print($format);
       ?>

    Output

    24-09-2019 13:59:30

    43. date_timestamp_get()

    This function will allow you to provide the Unix timestamp for the given DateTime object. This function will take one parameter and is mandatory.

    Syntax

    date_timestamp_get(object)

    Example

    <?php    
       //$date_time_Obj = date_create("25-09-1989");
       $date = date_modify(new DateTime(), "+15 day");  
       $timestamp = date_timestamp_get($date);
       print("Timestamp: ".$timestamp);
       ?>

    Output

    Timestamp: 1597292173

    44. date_timestamp_set()

    This function will take up two mandatory parameters- DateTime object and the Unix timestamp. This function will then set the provided timestamp for the given DateTime object.

    Syntax

    date_timestamp_set($object, $timestamp)

    Example

    <?php     
       //$date_time_Obj = date_create("25-09-1989");
       $date = date_modify(new DateTime(), "+15 day");  
       $res = date_timestamp_set($date, 1505292545);   
       print("Date: ".date_format($res, "Y/m/d H:i:s"));
    ?>

    Output

    Date: 2017/09/13 08:49:05

    45. date_get_last_errors()

    This function will provide you with the occurred errors or any warning while the function parses the date string. This function will not take any parameters and return the details in the array format.

    Syntax

    date_get_last_errors();

    Example

    <?php
       //$date_time_Obj = date_create("25-09-1989");
       //$date = date_modify(new DateTime(), "+15 day");  
       date_create("215-7896-848");
       $errors = date_get_last_errors();
       print_r($errors);
       ?>

    Output

    Date: 2017/09/13 08:49:05

    46. date_interval_create_from_date_string()

    This function takes up one mandatory parameter that is the interval string and provide you with the DateInterval object.

    Syntax

    date_interval_create_from_date_string($time)

    Example

    <?php    
       //$date_time_Obj = date_create("25-09-1989");
       //$date = date_modify(new DateTime(), "+15 day");  
       $time = "3year + 3months + 26 day + 12 hours+ 30 minutes +23 seconds";
       $interval = date_interval_create_from_date_string($time);
       print_r($interval);
       ?>

    Output-

    DateInterval Object ( [y] => 3 [m] => 3 [d] => 26 [h] => 12 [i] => 30 [s] => 23 [f] => 0 [weekday] => 0 [weekday_behavior] => 0 [first_last_day_of] => 0 [invert] => 0 [days] => [special_type] => 0 [special_amount] => 0 [have_weekday_relative] => 0 [have_special_relative] => 0 )

    47. date_interval_format()

    This function will take two mandatory parameters that are interval and the format string then this function will allow you to format the specified interval on the basis of the provided format.

    Syntax

     date_interval_format($interval, $format)

    48. date_create_immutable()

    This function will take two parameters that are optional which are the date/time string and the timezone. This function will then allow creating the DateTimeImmutable object. If there is no date/time string is provided then by default this function will create the object for the current date/time string.

    Syntax

    date_create_immutable([$date_time, $timezone]);

    Example

    <?php    
       //$date_time_Obj = date_create("25-09-1989");
       //$date = date_modify(new DateTime(), "+15 day");  
       $date_string = "2019-08-15 9:25:45";
       $immutable = date_create_immutable($date_string);   
       print_r($immutable);
    ?>

    Output

    DateTimeImmutable Object ( [date] => 2019-08-15 09:25:45.000000 [timezone_type] => 3 [timezone] => UTC )

    People are also reading: