PHP Date and Time


The PHP date() function serves for formatting a date or time.

You can also search for these topics, php date and time functions, php date and time format, php date and time picker, php date and time to timestamp, php date and time with timezone.

The PHP Date() Function

The PHP date() method converts timestamp to a date and time that is more legible.

Syntax :-

date(format,timestamp)
Parameter Description
format Required.The timestamp's format is specified here.
timestamp Optional.A timestamp is specified.

A timestamp is a character sequence that indicates the date and/or time at which a given event took place.

You can also search for these topics, php date format, php date to timestamp, php date parameters, php date format change, php date function, php date() function syntax.

Get a Date

The needed date() format argument defines how the date is formatted (or time).

The following are some examples of date characters :-

  • d - The month's day is represented by this symbol (01 to 31).
  • m - A month is represented by this symbol (01 to 12).
  • Y - A year is represented by this symbol (in four digits).
  • l (lowercase 'L') - The weekday is represented by this symbol.

Additional formatting can be added by inserting characters such as "/", ".", or "-" between the text.

Example :- Today's date is formatted three different ways in the example below :

<?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 2021/10/27
Today is 2021.10.27
Today is 2021-10-27

You can also search for these topics, php get date, php get date from datetime, php get date to timestamp, php get date format, php get a date characters, php get a date example.

PHP Tip - Automatic Copyright Year

To automatically change the patent year from your website, use the date() function :

Example :- To keep your website's copyright year current, use the date() function :

© 2010-<?php echo date("Y");?>

Output :-

© 2010-2021

You can also search for these topics, program to run automatic copyright year using php, which function update the copyright year in your website, automatic copyright year using php, php automatic copyright year example, php generate automatic copyright year format.

Get a Time

Here are some examples of characters that are often used to indicate the passage of time :

  • H - An hour is formatted in a 24-hour format (00 to 23).
  • h - Leading zeros are used in a 12-hour format (01 to 12).
  • i - Leading zeros in minutes (00 to 59).
  • s - Leading zeros in seconds (00 to 59).
  • a - Ante meridiem and Post meridiem are both written in lowercase letters (am or pm).

Example :- The following example displays the current time in the format you specify :-

<?php
echo "The time is " . date("H:i");
?>

Output :-

Servers current time is : 0:7

It should be noted that the PHP date() method returns the server's current date and time!.

You can also search for these topics, php get time, php get time from datetime, php get timestamp, php get time format, php get a time characters, php get a time example, php get a time in milliseconds.

Get Your Time Zone

If the time returned by the code is incorrect, it is most likely due to your server being located in another country or being configured for a different timezone.

So you may set the timezone you wish to utilize if you need to be right according to one particular region.

Example :- The example above sets "America/New York" to the time zone, and then the current time is in the format requested :

<?php
date_default_timezone_set("America/New_York");
echo "The time is " . date("h:i:sa");
?>
You can also search for these topics, php get your timezone, php get timezone offset, php get timezone name, php get default timezone, php get your timezone example, php get timezone from date and time.

Create a Date With mktime()

The following example changes the timezone to "America/New York" and then prints the current time in the format given.

For a date, the Unix timestamp is returned with the PHP mktime() feature. The time template for Unix includes the number of seconds (1 January 1970 00:00:00 GMT) between the Unix Epoque and the time.

Syntax :-

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

Example :- The following example uses the date() function to generate a date and time from the mktime() method's parameters :

<?php
$d=mktime(11, 14, 54, 8, 12, 2014);
echo "Created date is " . date("Y-m-d h:i:sa", $d);
?>
You can also search for these topics, php create a date with mktime(), syntax for create a date with mktime() using php, php create a date with mktime example, php mktime() fuction to create a date, php mktime() function.

Create a Date From a String With strtotime()

PHP strtotime() is used to convert a date string understandable by humans to a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT).

Syntax :-

strtotime(time, now)

Example 1 :- From the strtotime() function, the following example creates a date and time :

<?php
$d=strtotime("10:30pm April 15 2014");
echo "Created date is " . date("Y-m-d h:i:sa", $d);
?>

Example 2 :- PHP does a great job of converting a string to a date, allowing you to use a variety of values :

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

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

$d=strtotime("+3 Months");
echo date("Y-m-d h:i:sa", $d) . "<br />";
?>

Strtotime() isn't ideal though, so remember to verify the strings.


More Date Examples

Example 1 :- The following example shows the dates for the next six Saturdays :

<?php
$startdate=strtotime("Saturday");
$enddate=strtotime("+6 weeks", $startdate);

while ($startdate < $enddate) {
  echo date("M d", $startdate) . "<br />";
  $startdate = strtotime("+1 week", $startdate);
}
?>

Example 2 :- The following example calculates the number of days remaining till July 4th :

<?php
$d1=strtotime("July 04");
$d2=ceil(($d1-time())/60/60/24);
echo "There are " . $d2 ." days until 4th of July.";
?>
You can also search for these topics, php create a date from a string with strtotime(), php strtotime() function, php Create a Date From a String With strtotime() example, syntax for create a date from string with strtotime(), php strtotime() funtion timestamp.