Home >>Advance PHP Tutorial >PHP Date and Time

PHP Date and Time

PHP Date and Time

PHP Date and Time are the functions that generally permit the user to obtain the date and time from the server where the PHP script runs. The user can use the date/time functions in order to format the date and time in various ways.

Please note that these functions generally are dependent on the locale settings of the user's server. Make sure to take the daylight saving time and leap years into consideration while working with these functions of the PHP language.

Let's understand each of the functions that are included in the date and time functions of the PHP.

The PHP Date() Function

The PHP date () function is generally used to format a timestamp towards a efficient readable date and time.

Here is the syntax of the same:
date(format,timestamp)
Parameter Description
format This is required as this specifies the format of the timestamp
timestamp This is basically optional as it specifies a timestamp and the current date and time are known to be the default.

Get a Date in PHP

The way to format a date is generally decided by the required format parameter of the date() function.

Here are some of the characters that are most commonly used for dates that are depicted below:

  • d – This generally represents the day of the month (01 to 31)
  • m - This generally represents a month (01 to 12)
  • Y - This generally represents a year (in four digits)
  • l (lowercase 'L') - This generally represents the day of the week

There are some other characters, like"/", ".", or "-" that can also be inserted between the characters in order to add additional formatting.

Here is an example of the same that has depicted the three format of dates:
<?php
echo "Today is " . date("Y/m/d") . "<br>";
echo "Today is " . date("Y.m.d") . "<br>";
echo "Today is " . date("Y-m-d") . "<br>";
echo "Today is " . date("l");
?>
Output :
Today is 2020/01/30
Today is 2020.01.30
Today is 2020-01-30
Today is Thursday

Automatic Copyright Year

In order to update the copyright year automatically on any website, use the date () function.

Here is an example of the same:
© 2010-<?php echo date("Y");?>
Output :© 2010-2020

Get a Time

There are various characters that are used to display the time in PHP, some of them are depicted below:

  • H – This is generally used for the 24-hour format of an hour (00 to 23)
  • h - This is generally used for 12-hour format of an hour with leading zeros (01 to 12)
  • i - This is generally used for minutes with leading zeros (00 to 59)
  • s - This is generally used for seconds with leading zeros (00 to 59)
  • a - This is generally used for lowercase Ante meridiem and Post meridiem (am or pm)
Here is an example that is depicting the current time in a specified format:
<?php
echo "The time is " . date("h:i:sa");
?>
Output :The time is 12:26:43pm

Get Your Time Zone

There are some times when the time the user gets back from the code is generally not correct and it happens because the server might be in another country or the set up is for a different time zone. Hence, in order to get the time to be correct and according to a specific location the user can set the time zone according to their use.

Here is an example that basically sets the timezone to "America/New_York:
<?php
date_default_timezone_set("America/New_York");
echo "The time is " . date("h:i:sa");
?>
Output :The time is 06:28:43am

Create a Date With mktime()

The optional timestamp parameter that exists in the date() function basically specifies a timestamp. The current date and time will be used in case this is edited from the code.

The PHP mktime() function is known to return the Unix timestamp that is for a date. The number of seconds that exists between the Unix Epoch (January 1 1970 00:00:00 GMT) and the time specified is generally included in the UNIX timestamp.

Here is the syntax of the same:
mktime(hour, minute, second, month, day, year)
Here is an example
<?php
$date=mktime(11, 14, 54, 5, 30, 2019);
echo "Here is Created date " . date("Y-m-d h:i:sa", $date);
?>

Creating a Date From a String With strtotime() in PHP

The PHP strtotime() function is a function that is generally used in order to convert a human readable date string into a Unix timestamp that is the number of seconds since January 1 1970 00:00:00 GMT.

Syntax
strtotime(time, now)
Example

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

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

$date3=strtotime("+3 Months");
echo date("Y-m-d h:i:sa", $date3) . "<br>";
?>
Output :
2020-01-31 12:00:00am
2020-02-01 12:00:00am
2020-04-30 12:47:23pm