The PHP date()
function formats helps us to easily solved date related query in PHP.
date(“format”, timestamp) Returns a string according to the given format. Just read the whole blog and you will learn how to get the desired date format by just passing the string and format to PHP date() function.
You often have to work with dates and times during web development. like, you might need to display current data, yesterday’s date, or something else according to the situation.
Here comes PHP which makes things easy with its built-in date-time functions which help us to perform many tasks easily.
We will let you know about the complete date and time format used in PHP with examples. You will also learn how to convert String to Date and DateTime and how to add, subtract day, months, weeks, and year in date.
More about the date and time format
The required format parameter of the date() function specifies how to format the date (or time).
Below are some characters that are commonly used for dates:
- d – Represents the day of the month (01 to 31)
- m – Represents a month (01 to 12)
- Y – Represents a year (in four digits)
- l (lowercase ‘L’) – Represents the day of the week
Below is the table to show most common date format characters and their values.
Character | Meaning | Example |
---|---|---|
d | day of the month with leading zeros | 03 or 17 |
j | day of the month without leading zeros | 3 or 17 |
D | day of the week as a three-letter abbreviation | Mon |
l | full day of the week | Monday |
m | month as a number with leading zeros | 09 or 12 |
n | month as a number without leading zeros | 9 or 12 |
M | month as a three-letter abbreviation | Sep |
F | full month | September |
y | two-digit year | 18 |
Y | full year | 2018 |
Let’s see some examples of the date() function. We can use it to get the current year, current month, current hour, etc. by just passing the predefined character format.
<?php echo "Today is " . date("Y/m/d"); // Today is 2020/07/17 echo "Today is " . date("Y.m.d"); // Today is 2020.07.17 echo "Today is " . date("Y-m-d"); // Today is 2020-07-17 echo "Today is " . date("l"); // Today is Friday ?>
We can also use the date() function to show the time. Below is the table to show the most common time format characters and their values.
Character | Meaning | Example |
---|---|---|
g | hours in 12-hour format without leading zeros | 1 or 12 |
h | hours in 12-hour format with leading zeros | 01 or 12 |
G | hours in 24-hour format without leading zeros | 1 or 13 |
H | hours in 24-hour format with leading zeros | 01 or 13 |
a | am/pm in lowercase | am |
A | am/pm in uppercase | AM |
i | minutes with leading zeros | 09 or 15 |
s | seconds with leading zeros | 05 or 30 |
Below are some examples of time format in PHP date() function.
<?php echo date('h:i:s A'); // 09:25:30 AM echo date('l, h:i:s A'); // Friday, 09:25:30 AM echo date('d F Y, h:i:s A'); // 17 July 2020, 09:25:30 AM ?>
Common date format used in PHP
When we develop a website we encountered some most used PHP date format. We have listed down all those common date formats.
<?php echo date("F j, Y, g:i a"); // July 17, 2020, 8:47 am echo date("m.d.y"); // 07.17.20 echo date("j, n, Y"); // 17, 7, 2020 echo date("Ymd"); // 20200717 echo date('\i\t \i\s \t\h\e jS \d\a\y.'); // it is the 17th day. echo date("D M j G:i:s T Y"); // Fri Jul 17 8:48:07 CEST 2020 echo date('H:m:s \m \i\s\ \m\o\n\t\h'); // 09:05:39 m is month echo date("H:i:s"); // 08:48:07 echo date("Y-m-d H:i:s"); // 2020-07-17 08:48:07 (MySQL DATETIME format) ?>
Convert a Datetime String to a Timestamp
The strtotime() function is very helpful when you want to convert different time and date values in the string to a timestamp. this function can convert commonly used string into valid timestamp value. Find the related example below.
<?php $time = strtotime("5 months 10 days 8 hours ago"); echo 'It was '.date('l', $time).' on '.date('d F, Y h:i:s', $time).'.'; // Output - It was Friday on 07 February, 2020 02:09:25. $time = strtotime("next month"); echo 'It is '.date('l', $time).' on '.date('d F, Y h:i:s', $time).'.'; //Output - It is Monday on 17 August, 2020 09:27:27. $time = strtotime("third monday"); echo 'Date on the third monday from now will be '.date('d F, Y', $time).'.'; // Output - Date on the third monday from now will be 03 August, 2020. $time = strtotime("last day of December 2022"); echo 'Last day of December 2022 will be '.date('l', $time).'.'; // Output - Last day of December 2022 will be Saturday. ?>
PHP add 1 day to the current date
<?php $today = date('d-m-Y'); $tomorrow = date('d-m-Y', strtotime($today . ' +1 day')); echo 'date after adding 1 day: ' . $tomorrow; ?>
PHP subtract 1 day to the current date
<?php $today = date('d-m-Y'); $yesterday = date('d-m-Y', strtotime($today . ' -1 day')); echo 'date after adding 1 day: ' . $yesterday; ?>
PHP add 1 week to the current date
<?php $today = date('d-m-Y'); $nextWeek = date('d-m-Y', strtotime($today . ' +1 week')); echo 'date after adding 1 day: ' . $nextWeek; ?>
PHP subtract 1 week to the current date
<?php $today = date('d-m-Y'); $previousWeek = date('d-m-Y', strtotime($today . ' -1 week')); echo 'date after adding 1 day: ' . $previousWeek; ?>
PHP add 1 month to the current date
<?php $today = date('d-m-Y'); $nextMonth = date('d-m-Y', strtotime($today . ' +1 month')); echo 'date after adding 1 day: ' . $nextMonth; ?>
Subtract 1 month from a given date in PHP
<?php $today = date('d-m-Y'); $previousMonth = date('d-m-Y', strtotime($today . ' -1 month')); echo 'date after adding 1 day: ' . $previousMonth; ?>
You can change the date format by passing the desired format in date() function.