PHP DateTime represents the date and time, and this tutorial will reveal the ways you can add and format date and time using PHP.
The functions for date manipulation are most widely used when designing websites or executing certain SQL queries. PHP current date might be displayed at the top of the page. Every entry can be dated and every comment marked with the exact time. In PHP time and date are easy to set, as there are inbuilt functions for that.
When working with the functions to receive PHP DateTime, remember to pay attention to the usual time inconsistencies that affect your local server. Don't forget about different timezones, daylight savings, or leap years.
Contents
PHP DateTime: Main Tips
- The PHP
date()
function is needed to format date/time in your script. - It is used to format the timestamp to be more readable. Basically, to line the characters in a more eye-pleasing order.
- Use
mktime()
function when you need an Unix date timestamp. - You can use PHP functions to make automatic updates on the copyright date of your website.
date() Function Explained
To learn PHP date formatting, let's first look at the correct syntax of date()
function:
date(format,timestamp)
The format part is required, as it specifies the format of the timestamp. Entering the timestamp itself is optional: if you don't, PHP current date and time will be displayed by default.
There is a set of characters more commonly used to describe a PHP date format.
- d - indicates the day of the month (01-31)
- m - indicates a month (01-12)
- Y - indicates a year (in four digits)
- l (lowercase 'L') - indicates the day of the week
Symbols like a slash (/), a dot (.), or a dash (-) can be added for additional formatting.
Let's look at an example below. You can see there are four different ways to do PHP date formatting:
<?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");
?>
Update Dates Automatically
Stuff that requires to be updated the rarest is usually the first to be forgotten. This often happens with the copyright dates deep at the bottom of the website. Luckily, there are ways to avoid this.
To automatically refresh the copyright year on your website, you can use the function displayed in the example script below:
Time Functionalities
We already indicated a set of symbols that are most commonly used for PHP date formatting. There's also a handful of symbols used specifically for displaying time:
- h - 12-hour format of an hour with leading zeros (01-12)
- i - minutes with leading zeros (00-59)
- s - seconds with leading zeros (00-59)
- a - AM/PM (before/after noon)
Using the script example below will output the current time in the specified format:
Note: Have in mind the date() function will fetch back the present PHP DateTime of the server.
- Easy to use with a learn-by-doing approach
- Offers quality content
- Gamified in-browser coding experience
- The price matches the quality
- Suitable for learners ranging from beginner to advanced
- Free certificates of completion
- Focused on data science skills
- Flexible learning timetable
- Simplistic design (no unnecessary information)
- High-quality courses (even the free ones)
- Variety of features
- Nanodegree programs
- Suitable for enterprises
- Paid Certificates of completion
- A wide range of learning programs
- University-level courses
- Easy to navigate
- Verified certificates
- Free learning track available
- University-level courses
- Suitable for enterprises
- Verified certificates of completion
Getting Around Timezones
Does the PHP DateTime you are getting not match your current time? It might happen if you are using a server that is located in a different timezone than you. It's not that hard to fix: you need to set a correct PHP timezone for your DateTime format to use.
Look at the example below. You can see that the PHP timezone is set to America/New_York
, which means the script will output PHP time that is relevant to New Yorkers.
<?php
date_default_timezone_set("America/New_York");
echo "The time is " . date("h:i:sa");
?>
mktime() Method Defined
One more function that you should learn to use to display PHP date time is mktime()
. It is used to fetch back the Unix date timestamp.
Unix date timestamp is a long integer that displays the time (in seconds) between the Unix Epoch (1st of January 1, 1970, 00:00:00 GMT) and the time specified. In the syntax formula below you can see the function parameters: hours, minutes, seconds, months, days, and years:
mktime(hour,minute,second,month,day,year)
Look at the script below to see how the PHP date time is created using parameters from mktime()
:
<?php
$date = mktime(11, 14, 54, 8, 12, 2014);
echo "Created date is " . date("Y-m-d h:i:sa", $date);
?>
strtotime(): From String to PHP Date
The strtotime()
function changes a readable string to a Unix time. This is how its syntax looks like:
strtotime(time,now)
In the block of code below, PHP date time is created using strtotime()
:
<?php
$d = strtotime("10:30pm April 15 2014");
echo "Created date is " . date("Y-m-d h:i:sa", $d);
?>
PHP is very intuitive when converting strings to dates. Therefore, you can try and input many 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>";
?>
PHP DateTime: Summary
- Date/time format in PHP codes can be set using
date()
function. It will also makes the timestamp more readable. - When Unix date timestamp is needed, PHP
mktime()
function should be chosen. - PHP DateTime format can be used to make automatic updates on website copyright dates.