Date and time in PHP

Photo by Srikanta H. U on Unsplash

Date and Time in PHP

PHP Dec 24, 2022

One of the functions that is used on almost every website is the date( ... ) function . The date function is needed both on large news sites and on small personal blogs. But the output of the date in one format is far from the only purpose of this function.

date function in php

Let's try to experiment with this function and display dates in completely different formats. Including not current dates, but dates in a few days, hours or seconds.

Let's start with an example:

<?php
   $a = date('d.m.Y H:i:s');
   echo $a;
?>

As a result of executing this code, the current date (the date on which the server is running) will appear in the browser in the format "DD.MM.YYYY HH:MM:SS". As you might guess, each character in the date( ... ) brackets outputs a part of the date in its own format. Dots and colons are wrapped as is. Let's analyze some of the possible and most popular variations:

  • d is the number of the day in the month. If less than 10, then with zero: "09", "05"
  • m - month number. If less than 10, then with zero: "09", "05"
  • Y - year, 4 digits.
  • y - year, two digits.
  • n is the number of the month. No leading zero if less than 10
  • j is the number of the day in the month. No leading zero if less than 10
  • H - hours in 24-hour format. If less than 10, then with zero: "09", "05"
  • s - seconds. If less than 10, then with zero: "09", "05"
  • i - minutes. If less than 10, then with zero: "09", "05"
  • z - day number from the beginning of the year.
  • w - day of the week (0 - Sunday, 1 - Monday, etc.).
  • h - hours in 12-hour format
  • L - 1 if it's a leap year, 0 if it's not a leap year.
  • W - serial number of the week of the year.
  • U is the number of seconds since January 1, 1970 (i.e. timestamp).Pay attention to the last value in this list "U - timestamp". It's used so often that it has its own time( ... ) function . This function returns the number of seconds since January 1, 1970. For example, "1560759834". And with the help of the second argument of the date( ... ) function, you can get not only the current date of the server, but also convert the number of timestamp seconds into a date. Let's give an example in which we calculate the date in 3 days relative to the current date:
<?php
   $a = time() + 3600 * 24 * 3; // There are 3600 seconds in an hour. There are 24 hours in a day. And it takes 3 days.
   echo date('d.m.Y H:i:s', $a);
?>

As a result of executing this code, we will find out the date and time in the format "DD.MM.YYYY HH:MM:SS" in 3 days. Or speaking in seconds, after 3600 * 24 * 3 seconds. You can not only add, but also subtract seconds to get a date in the past. This method of calculating the date is one of the simplest, but for full use, you still need a function that converts a date from any format to a timestamp. Such a function exists and is called strtotime( ) . It takes a date in any format as an argument. But it's better to use obvious formats like "DD.MM.YYYY HH:MM:SS". Then she will correctly figure out what belongs to the year and what to the day. Let's try it in practice:

<?php
   $a = '20.06.2019'; // equals date with zero time '20.06.2019 00:00:00'
   echo strtotime($a); // result 1560978000

   $a = '20.06.2019 14:16:29';
   $b = strtotime($a);
   echo $b; // result 1561029389
   echo date('d.m.Y H:i', $b); // result 20.06.2019 14:16
   echo date('d-m', $b); // result 20-06

   $b += 3600 * 24 * 3; // add three days
   echo date('d.m.Y', $b); // result 23.06.2019

   $b -= 3600 * 24 * 3; // take three days
   echo date('d.m.Y', $b); // result 20.06.2019
?>

checkdate()

Validates Gregorian dates.

date_add()

Add days, months, years, hours, minutes, and seconds to a date.

date_create_from_format()

Returns a new DateTime object according to the specified format.

date_create()

Returns a new DateTime object.

date_date_set()

Set a new date.

date_default_timezone_get()

Returns the default timezone used by all date/time functions in the script.

date_default_timezone_set()

Sets the default timezone used by all date/time functions in the script.

date_diff()

Returns the difference between two dates.

date_format()

Returns a date formatted according to the specified format.

date_get_last_errors()

Returns warnings and errors found while parsing a date/time string.

date_interval_create_from_date_string()

Set a DateInterval from the relevant part of the string.

date_interval_format()

Format interval.

date_isodate_set()

Sets the date according to the ISO 8601 standard.

date_modify()

Modification timestamp.

date_offset_get()

Returns the time zone offset.

date_parse_from_format()

Returns an associative array containing the details of the given date formatted according to the specified format.

date_parse()

Returns an associative array with details for the specified date.

date_sub()

Subtracts days, months, years, hours, minutes, and seconds from a date.

date_sun_info()

Returns an array containing information about sunset/sunrise and twilight start/end for the specified date and location.

date_sunrise()

Returns the sunrise time for a given date and location.

date_sunset()

Returns the sunset time for a given date and location.

date_time_set()

Set the time.

date_timestamp_get()

Returns a Unix timestamp representing a date.

date_timestamp_set()

Set the date and time according to a Unix timestamp.

date_timezone_get()

Returns the time zone relative to the given datetime.

date_timezone_set()

Sets the time zone for a DateTime object.

date()

Format a local date and time.

getdate()

Returns a timestamp or date/time information for the current local date/time.

gettimeofday()

Returns the current time.

gmdate()

Format GMT/UTC date and time.

gmmktime()

Get the Unix timestamp of a GMT date.

gmstrftime()

Format the GMT/UTC date and time according to the locale.

idate()

Format a local time/date as an integer.

localtime()

Returns the local time.

microtime()

Returns the current Unix timestamp in microseconds.

mktime()

Returns the Unix timestamp of a date.

strftime()

Formats the local time/date according to the locale.

strptime()

Parse a time/date generated using strftime().

strtotime()

Parses an English text datetime as a Unix timestamp.

time()

Return the current time as a Unix timestamp.

timezone_abbreviations_list()

Returns an associative array containing dst, offset and timezone name.

timezone_identifiers_list()

Returns an indexed array containing all defined time zone identifiers.

timezone_location_get()

Returns the location information for the specified time zone.

timezone_name_from_abbr()

Return the time zone name from the abbreviation.

timezone_name_get()

Returns the time zone name.

timezone_offset_get()

Returns the time zone offset from GMT.

timezone_open()

Creates a new DateTimeZone object.

timezone_transitions_get()

Returns all conversions for the time zone.

timezone_version_get()

Returns the current version of timezonedb.

Tags

Anurag Deep

Logical by Mind, Creative by Heart