php date

PHP Date: Creation and Formatting

Used before category names. Blog PHP

The date() function is used to format date and time in PHP. The PHP date, like all other programming languages, require conversion and formatting. Even for different time zones, different dates need to be used.

The date is used in two ways in PHP. One is a timestamp that displays the number of seconds since January 1, 1970, from 00:00:00 GMT, and the other is a string that we use every day and is understandable to humans.

Get PHP date by the date() function

This function returns a string containing the date based on the given format and timestamp.

The general format of the date() function is as follows.

date(format,timestamp) 
  • format: Specifies the timestamp format.
  • timestamp: Specifies the timestamp. The default time is the current date and time. This parameter is optional.
echo "Today is: " . date("Y/m/d");
---------------------
Today is: 2022/08/13
echo "Today is: " . date("Y-m-d");
-------------------
Today is: 2022-08-13

The format parameter specifies how to format the date or time. The formatting must be according to the PHP standard, which you can see here.

In date and time formatting, each character has its own meaning, which is explained below:

  • Y: display the year in four digits (2022)
  • y: showing the year in two digits (22)
  • M: display the name of the month in three letters (Aug)
  • m: display the month in numbers (01 – 12) (08)
  • n: display the month as a number without zero (1 – 12) (8)
  • F: display the full name of the month in letters (August)
  • D: showing the day of the week in three letters (Sat)
  • w: display the day of the week without zero (0 – 6)
  • d: Display the day of the month (01 – 31) (13)
  • j: display the day of the month without zero (1 – 31) (5)
  • S: Add an English suffix like “th” (10th)
  • H: Displays the time in 24 hours (00 – 24) (17)
  • h: 12-hour clock display (01 – 12 plus zero) (05)
  • G: 24-hour clock display without zero (0 – 24) (17)
  • g: display the clock as 12 hours without zero (1 – 12) (5)
  • i: display minutes (00 – 59) (24)
  • s: display seconds (00 – 59) (36)
  • a: display am and pm in lowercase letters (pm)
  • A: Display AM and PM in capital letters (PM)
  • t: number of days in the month (28 – 31) (February has 28 days)
  • L: Returns 1 if it was a leap year and 0 if not
  • l: showing the day of the week in letters (small L) (Saturday)
  • z: day of the year without zero (0 – 365)
  • U: number of seconds since January 1, 1970 (same as a timestamp in Unix)
  • O: Displays the time zone offset from Greenwich
echo "Today is: " . date("Y-m-d H:i:s a => l") . "<br/>";
echo "Today is: " . date("Y-m-d h:i:s A");
--------------------
Today is: 2022-08-13 14:33:57 pm => Saturday
Today is: 2022-08-13 02:33:57 PM

More Read: PHP Date Format

Definition of time zone

The date() function returns the date and time of the server. If you run the code on the server, the return time may be different from the time of your region, this is because the server may be is in another country. To solve this problem, we set our time zone to PHP date.

date_default_timezone_set("Australia/Sydney");
echo "Today is: " . date("Y-m-d h:i:s A");
-------------------------------
Today is: 2022-08-14 12:56:15 AM

In the example above, we changed the time zone to Sydney. To get the time zones, you can get them from here.

time() function

As the name of the function suggests, this function is used to get the current time in Unix format.

echo "Time is: " . time() . "<br/>";
echo date("Y m d h:i:s", time());
----------------
Time is: 1660458590
2022 08 14 06:29:50

In the example, time() and date() functions are used to create date and time.

Create date with mktime() function

This function returns the Unix time. This time returns the number of seconds since January 1, 1970, at 00:00:00 GMT.

The general format of the mktime() function is as follows.

mktime(hour, minute, second, month, day, year)
$da = mktime(11, 14, 54, 8, 13, 2022);
echo "The date is " . date("Y-m-d h:i:sa", $da);
------------------
The date is 2022-08-13 11:14:54am

In the example above, we have used date() and mktime() functions to create a date.

Creating a date in PHP from a string using the strtotime() function

You cannot tell what day it is from the number of seconds, which is a big number, and the computer cannot read the date that humans use. This function is used to convert a date that humans use to a Unix date.

strtotime(time, now)
$da = strtotime("10:30pm August 14 2022");
echo "The date is " . date("Y-m-d h:i:sa", $da);
--------------------
The date is 2022-08-14 10:30:00pm

This example converts a regular date to a Unix date.

As we said, this function is used to convert normal dates, so you can use it in the following examples.

$da=strtotime("tomorrow");
echo date("Y-m-d h:i:sa", $da) . "<br>";

$da=strtotime("next Saturday");
echo date("Y-m-d h:i:sa", $da) . "<br>";

$da=strtotime("+3 Months");
echo date("Y-m-d h:i:sa", $da);
-----------------------
2022-08-15 12:00:00am
2022-08-20 12:00:00am
2022-11-14 05:52:08am

The only thing you should pay attention to is that the letters inside this function must be written correctly.

DateTime class

This class is used to display the current date and time.

var_dump(new DateTime());
----------------
object(DateTime)[1]
  public 'date' => string '2022-08-14 07:34:57.729491' (length=26)
  public 'timezone_type' => int 3
  public 'timezone' => string 'UTC' (length=3)
$now = new DateTime();
echo $now->format("Y-m-d h:i:s A");
----------------
2022-08-14 07:47:02 AM

This example works like the date() function.

echo $now->getTimestamp();
------------------
1660463345

This example works like the time() function.

Date and time can be created using the DateTime class like mktime() function.

$date = new DateTime("2015-07-14");
var_dump($date);
-------------------
object(DateTime)[1]
  public 'date' => string '2015-07-14 00:00:00.000000' (length=26)
  public 'timezone_type' => int 3
  public 'timezone' => string 'UTC' (length=3)

or

$date = new DateTime();
$date->setTimestamp(1634142890);
var_dump($date);
----------------
object(DateTime)[1]
  public 'date' => string '2021-10-13 16:34:50.000000' (length=26)
  public 'timezone_type' => int 3
  public 'timezone' => string 'UTC' (length=3)

or

$date = DateTime::createFromFormat('F-d-Y h:i A', 'April-18-1973 9:48 AM');
$new_date_format = $date->format('Y-m-d H:i:s');
echo $new_date_format;

Now suppose we want to apply the time zone as above. For this, we do the following in the DateTime class.

$timezone = new DateTimeZone("Australia/Sydney");
$date = new DateTime("2021-10-13 05:00", $timezone);
var_dump($date);
-------------------
object(DateTime)[2]
  public 'date' => string '2021-10-13 05:00:00.000000' (length=26)
  public 'timezone_type' => int 3
  public 'timezone' => string 'Australia/Sydney' (length=16)

Add, subtract and compare PHP dates

It is possible to add, subtract and compare dates in PHP. For this, you can use date_add() and date_sub() functions for addition, date_diff() subtraction, and the difference between two dates.

Adding PHP dates

You can use the following method to add a date.

$Date = "2022-05-10";
echo date('Y-m-d', strtotime($Date. ' + 10 days'));
-----------------
2022-05-20

or

$date = date_create("2022-05-10");
date_add($date, date_interval_create_from_date_string("10 days"));
echo date_format($date, "Y-m-d");
----------------
2022-05-20

or

$date1 = '2022-05-10';
$date = new DateTime($date1);
$date->add(new DateInterval('P10D')); // P10D means a period of 10 day
$date2 = $date->format('Y-m-d');
echo $date2;
-----------------
2022-05-20

For more information about the “P10D” format, you can refer to here.

Subtraction of dates

There are two methods for the difference between two dates, using the DateTime class is easier than the other.

$now = date_create('now');
$future = date_create('last day of January 2024');
$interval = date_diff($now, $future);
echo $interval->format('%Y years, %M months and %d days');
------------------------
01 years, 05 months and 16 days
$now = new DateTime('now');
$future = new DateTime('last day of January 2024');
$interval = $now->diff($future);
echo $interval->format('%Y years, %M months and %d days');
------------------
01 years, 05 months and 16 days

Compare two dates in PHP

If the dates are both in the same format, comparing them is very simple. A problem arises when both are not in the same format, so we have to convert the format.

$date1 = "1998-11-24";
$date2 = "1997-03-26";

if ($date1 > $date2)
    echo "$date1 is latest than $date2";
else
    echo "$date1 is older than $date2";
-------------------
1998-11-24 is latest than 1997-03-26

If the formats are not the same, we convert the format using the mentioned methods.

$date1 = "12-03-26";
$date2 = "2011-10-24";

$dateTimestamp1 = strtotime($date1);
$dateTimestamp2 = strtotime($date2);

if ($dateTimestamp1 > $dateTimestamp2)
    echo "$date1 is latest than $date2";
else
    echo "$date1 is older than $date2";
-----------------------
12-03-26 is latest than 2011-10-24

Using the DateTime class

$date1 = new DateTime("12-11-24");
$date2 = new DateTime("2011-03-26");

if ($date1 > $date2)
    echo $date1->format("Y-m-d") . " is latest than " . $date2->format("Y-m-d");
else
    echo $date1->format("Y-m-d") . " is older than "  . $date2->format("Y-m-d");
--------------------
2012-11-24 is latest than 2011-03-26
Tags: