Understanding PHP Date and Time

In this article, we will examine PHP dates and times closely with examples. We will cover topics such as working with timezones, manipulating dates and times, and creating and formatting dates.

Date and time are essential parts for almost all web applications, and PHP offers a range of built-in features and functions for working with dates and times.

No matter what you need for formatting dates, calculating intervals between dates, displaying the current date, or displaying dates in different formats, PHP gives you all the features you need.

This post will help you build fast, reliable date and time functionality into your web applications using PHP date and time handling.



PHP Date() Function

With PHP date() function, you have the ability to format dates and times in multiple formats.

There are two arguments that are passed to the function:

  • The format string and the optional timestamp. In the format string, formatting characters are used to specify how the date should be formatted.
  • Timestamps are optional Unix timestamps that represent the date and time to format.
When there is no timestamp specified, the function uses the current date and time as default.

Syntax

date(format,timestamp)
ParametersOverview
formatThis is required. The format of the timestamp should be specified
timestampIt is optional. Provide a timestamp for the date. As a default, PHP date and time are set to the current date and time

The term “timestamp” refers to a sequence of characters that denote the date and/or time when an event took place.


Get Date

PHP Date() functions require a format parameter that specifies how to format the date (or time) when it is passed to them.

The following are some common characters that are used as part of a date:

Characters Overview
dThis value represents the day of the month (from 1st to 31st).
mThis is a representation of a month (01-12).
YRepresents an entire year (in four digits).
l(lowercase ‘L‘) – Indicates the day of the week that it is.

There are also other characters that can be inserted between the characters for adding additional formatting, such as / ., or .

In the example below, we show three different ways in which we can format today’s date:

Example: 

<?php echo "Date format in dashes: " . date("d-m-Y") . "<br>"; echo "Date format in dots: " . date("d.m.Y") . "<br>"; echo "Date format in slash: " . date("d/m/Y") . "<br>"; echo "Today is " . date("l"); ?>

In below example, let’s try different formats of dates using the date() function:

Example: 

<?php echo "Date format month/day/year: " . date("m/d/Y") . "<br>"; echo "Date format month/day, year: " . date("m/d, Y") . "<br>"; echo "Date format Year-month-day: " . date("Y-m-d") . "<br>"; ?>

PHP Date() Function


The following code is an example that can be used to automatically update the copyright year on your website using the date() function:

Example: 

© 1996-<?php echo date("Y");?>, Amazon.com, Inc. or its affiliates

Get Time

The following are some common characters that are used to indicate the time of day:

Characters Overview
HFormat of 24-hours of an hour (from 00 to 23).
hThe format in which a 12-hour period is expressed with a leading zero (01 to 12).
iThis represents the minutes of the hour with leading zeros (00 to 59).
sThis is the number of seconds with leading zeros (00-09).
aA lowercase ante meridiem and post meridiem (the time on the clock).

Following is an example of how the current time is outputted in the format specified below:

Example: 

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

Below is an example of current time in 12 hour format:

Example: 

<?php echo "Current time in 12 hour format: " . date("h:i:sa"); ?>
Remember: In PHP, when the date() function is called, it will return the current time and date that have been set on the server!

Now Get Time Zone

If you are getting an incorrect time back from the code, it may be because your server is located in another country or set up for a different time zone than what you expected.

So, if you want the time to be accurate based on the actual location of the user, you can set the timezone to correspond with that location.

There is an example below that sets the timezone to “America/New_York“, and then outputs the current time in the format specified:

Example: 

<?php date_default_timezone_set("America/New_York"); echo "The time in America/New_York zone is: " . date("h:i:sa"); ?>
In the example below, we can now set the timezone to “Europe/London” to check for a change in timezone:

Example: 

<?php date_default_timezone_set("Europe/London"); echo "The time in the Europe/London zone is: " . date("h:i:sa"); ?>

Date Through mktime()

PHP Date() function accept a timestamp as an optional parameter.

As in the examples above, ignoring this field will use the current date and time.

A date can be converted into a Unix timestamp using PHP mktime() function.

It is a simple method of representing the number of seconds that have passed since the Unix Epoch (January 1, 1970 00:00:00 GMT) and the time indicated.

Syntax

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

With the date() function and the mktime() function, the following date and time are created:

Example: 

<?php $d=mktime(9, 56, 21, 2, 23, 2023); echo "Combination of mktime() and Date() functions is: " . date("Y-m-d h:i:sa", $d); ?>
By using both mktime() and Date() functions we can set the date from the past like below:

Example: 

<?php $d=mktime(3, 4, 54, 12, 25, 2020); echo "Date from 2020: " . date("Y-m-d h:i:sa", $d); ?>

Date With strtotime()

With the help of PHP strtotime(), you can convert a string of human-readable date into a Unix timestamp (which indicates the number of seconds since (January 1, 1970 00:00:00 GMT).

Syntax

strtotime(time, now)

Here is an example of how to create a date and time using the strtotime() function:

Example: 

<?php $d=strtotime("03:04pm Dec 25 2020"); echo "Date from year 2020 conversion in date format: " . date("Y-m-d h:i:sa", $d); ?>
Using the strtotime() function we get today’s date in the example below:

Example: 

<?php $d=strtotime("today"); echo "Today's Date: " . date("Y-m-d h:i:sa", $d); ?>
Luckily, PHP is quite good at converting string values into dates.
This means that you are able to use a variety of input values, such as:

Example: 

<?php $d=strtotime("yesterday"); echo "Yesterday date: ".date("Y-m-d h:i:sa", $d) . "<br>"; $d=strtotime("tomorrow"); echo "Tomorrow date: ".date("Y-m-d h:i:sa", $d) . "<br>"; $d=strtotime("this saturday"); echo "This saturday date: ".date("Y-m-d h:i:sa", $d) . "<br>"; $d=strtotime("+1 Months"); echo "After 1 month: ".date("Y-m-d h:i:sa", $d) . "<br>"; ?>

Despite this, strtotime() isn’t a perfect function, so it’s best to check the strings before inserting them into it.

Below is an example that outputs PHP dates for the next six Sundays, as follows:

Example: 

<?php $start_date = strtotime("Sunday"); $end_date = strtotime("+6 weeks", $start_date); while ($start_date < $end_date) { echo date("M d", $start_date) . "<br>"; $start_date = strtotime("+1 week", $start_date); } ?>

Example Explanation

In above example we have demonstrate how to generate a list of dates for the next 6 weeks starting on the upcoming Sunday.
Here’s how the code works:

1 – The first line of code initializes a variable called $start-date with the value of the next upcoming Sunday, using the strtotime function to convert the string “Sunday” to a Unix timestamp.

2 – The second line of code initializes a variable called $end-date with a Unix timestamp that represents the date 6 weeks after $start-date.

  • This is accomplished using the strtotime function again, this time passing two arguments:
  • the first argument is a string representing a time period to add (in this case, “+6 weeks“).
  • The second argument is the starting Unix timestamp ($start-date).
  • The while loop iterates as long as $start-date is less than $end-date. In each iteration, it outputs the date represented by the Unix timestamp $start-date, formatted as a string using the date function.
  • The format used here is “M d“, which displays the abbreviated month name followed by the day of the month (e.g. “Mar 01“).

3 – After each iteration of the loop, the $startdate variable is updated to represent the date one week later than the current value of $start-date.

  • This is done using the strtotime function again, this time adding “+1 week” to the current value of $start-date.
  • This ensures that the loop will iterate through each Sunday for the next 6 weeks.
Here is an example of how to calculate the number of days until April 8:

Example: 

<?php $d1=strtotime("April 08"); $d2=ceil(($d1-time())/60/60/24); echo $d2 ." days left until April 8."; ?>
We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *