PHP Date & Strtotime Cheat Sheet with PDF
(Source/Credits: https://dev.to/danenglishby/php-date-strtotime-cheat-sheet-with-pdf-1ni8)
If you want to bookmark the original post, feel free with the following link - https://www.codewall.c...
If you want to bookmark the original post, feel free with the following link - https://www.codewall.co.uk/php-date-strtotime-cheat-sheet/
If you've been developing some projects that are data-rich like me, you will of seen yourself using the Date
function and the Date
function mixed with strtotime
, Date
& strtotime
are very powerful tools, enabling you to set date criteria very swiftly, no matter the scenario. It's simply all about knowing how to do it, as there are a lot of commands you can give these functions that are extremely useful. For example, '-7 days'. This article will be an online cheat sheet, but at the end of the article, there will be a download link with the most popular 'cheats' that can be printed perfectly on an A4 piece of paper. You can download the PDF Here or at the end of this page.
-
Current Date & Time Formatting
- 4 Digit Year
- 2 Digit Year
- Month Number
- Day Number
- 12 & 24 Hour Format Hour
- Minutes
- Seconds
- Date Time Stamp
- Custom Date & Time Formatting
- Date & Time Manipulation With strtotime
-
- First day of the month
- Last day of the month
- Subtract days from date
- Add days to date
- Subtract months from date
- Add months to date
- Subtract years from date
- Add years to date
- Subtract hours from date
- Add hours to date
- Subtract minutes from date
- Add minutes to date
- Subtract seconds from date
- Add seconds to date
- Get yesterdays date
- Get tomorrows date
- Get next date
- Get previous date
- Next day name
- Last day name
- First day of month name
- Last day of month name
- Noon - Midnight Date
Current Date & Time Formatting
Let's start with plain old Date formatting, there are hundreds of variations available, but you will probably only ever use a few! Please Note: All examples in this section work with the Current Date Time, meaning the real-time-stamp of your computer at the time of execution!
4 Digit Year (#4-digit-year)
```php
echo date('Y'); // 2019 ```
2 Digit Year
```php
echo date('y'); // 19
```
Month Number
```php
echo date('m'); // 08
```
Day Number
php
echo date('d'); // 26
Date With 2 Digit Year
```php
echo date('y-m-d'); // 19-08-26
```
Date With 4 Digit Year
```php
echo date('Y-m-d'); // 2019-08-26
```
12 Hour & 24 Hour Format Hours
Quick note: There are 2 ways to access the hour from the date() function, use the following parameters to yield an identical result -
date('g')
- (12 Hour Format) ordate('G')
- (24 Hour Format)-
date('h')
- (12 Hour Format) ordate('H')
- (24 Hour Format) ```phpecho date('h'); // 11 (am) (12 Hour Format Style) echo date('H'); // 11 (am) (24 Hour Format Style) echo date('g'); // 11 (am) (12 Hour Format Style) echo date('G'); // 11 (am) (24 Hour Format Style) ```
Minutes
```php
echo date('i'); // outputs: 08 (Minutes 00-59)
```
Seconds
```php
echo date('s'); // outputs: 56 (Seconds 00-59)
```
Date Time Stamp
Now with all of these parameters in thought, we can produce a proper date-time stamp in the format of YYYY-MM-DD HH:MM:SS ```php
echo date('Y-m-d h:i:s'); // 2019-08-26 09:48:01
```
And if we want to do it the other way, swapping the place of the year so that the format is DD-MM-YYYY HH:MM:SS ```php
echo date('d-m-Y h:i:s'); // 2019-08-26 09:48:01
```
Custom Date & Time Formatting With strtotime
Using a custom date or date-time with the date function is very straight forward. All's you need to do is pass in the date with the strtotime
function after the formatting parameters is set. See the example below, notice that the passed in date format is YYYY-MM-DD and that the actual output is DD-MM-YYYY because of the specified formatting. Example 1
```php
echo date('d-m-Y h:i:s', strtotime('2018-01-31 00:00:01')); // 31-01-2018 12:00:01
```
This exact methodology of using the date function will work with all the formatting displayed in the first section of this article, Example 2 ```php
echo date('Y-m-d', strtotime('2019-08-26')); // outputs 2019-08-05
```
So, the date function will format the strtotime
date passed into whichever format parameters are specified. So another example, to get the day of the month number from the date passed in goes like this - Example 3
```php
echo date('d', strtotime('2019-08-26')); // outputs 05
```
Date & Time Manipulation with strtotime
To be honest, strtotime
is an absolute godsend, being able to give it a parameter in a language that you actually speak is awesome. Eg. 'First day of this month' or 'First day of this week' and so on. This can be utilized without a custom date, so basically, the date-time of your server, or a date-time of your specification. In this next section, I will demonstrate both use-cases.
First day of the month
```php
// Using server date-time
echo date('Y-m-d h:i:s', strtotime('first day of this month')); // 2019-08-01 19:35:00
//Using custom date-time
echo date('Y-m-d h:i:s', strtotime('first day of this month', strtotime('2019-12-31 19:00:00'))); // 2019-12-01 07:00:00
```
Last day of the month
```php
// Using server date-time
echo date('Y-m-d h:i:s', strtotime('last day of this month')); // 2019-08-31 19:35:00
//Using custom date-time
echo date('Y-m-d h:i:s', strtotime('last day of this month', strtotime('2019-12-31 19:00:00'))); // 2019-12-01 07:00:00
```
Subtract days from date
```php
// Using server date-time
echo date('Y-m-d H:i:s', strtotime('-7 days')); // Outputs: 2019-08-19 11:43:59
//Using custom date-time
echo date('Y-m-d H:i:s', strtotime('-7 days', strtotime('2019-08-13 14:00:00'))); // Outputs: 2019-08-06 14:00:00
```
Add days to date
```php
// Using server date-time
echo date('Y-m-d H:i:s', strtotime('+7 days')); // Outputs: 2019-09-01 11:43:59
//Using custom date-time
echo date('Y-m-d H:i:s', strtotime('+7 days', strtotime('2019-08-13 14:00:00'))); // Outputs: 2019-08-20 14:00:00
```
Subtract months from date
```php
// Using server date-time
echo date('Y-m-d H:i:s', strtotime('-7 months')); // Outputs: 2019-01-26 11:56:06
//Using custom date-time
echo date('Y-m-d H:i:s', strtotime('-7 months', strtotime('2019-08-13 14:00:00'))); // Outputs: 2020-03-13 14:00:00
```
Add months to date
```php
// Using server date-time
echo date('Y-m-d H:i:s', strtotime('+7 months')); // Outputs: 2020-03-26 11:56:45
//Using custom date-time
echo date('Y-m-d H:i:s', strtotime('+7 months', strtotime('2019-08-13 14:00:00'))); // Outputs: 2020-03-13 14:00:00
```
Subtract years from date
```php
// Using server date-time
echo date('Y-m-d H:i:s', strtotime('-5 years')); // Outputs: 2014-08-26 11:57:16
//Using custom date-time
echo date('Y-m-d H:i:s', strtotime('-5 years', strtotime('2019-08-13 14:00:00'))); // Outputs: 2014-08-13 14:00:00
```
Add years to date
```php
// Using server date-time
echo date('Y-m-d H:i:s', strtotime('+7 years')); // Outputs: 2024-03-26 11:48:16
//Using custom date-time
echo date('Y-m-d H:i:s', strtotime('+7 years', strtotime('2019-08-13 14:00:00'))); // Outputs: 2024-03-13 14:00:00
```
Subtract hours from date
```php
// Using server date-time
echo date('Y-m-d H:i:s', strtotime('-12 hours')); // Outputs: 2019-08-25 23:21:23
//Using custom date-time
echo date('Y-m-d H:i:s', strtotime('-12 hours', strtotime('2019-08-13 14:00:00'))); // Outputs: 2019-08-13 02:00:00
```
Add hours to date
```php
// Using server date-time
echo date('Y-m-d H:i:s', strtotime('+12 hours')); // Outputs: 2019-08-26 23:21:53
//Using custom date-time
echo date('Y-m-d H:i:s', strtotime('+12 hours', strtotime('2019-08-13 14:00:00'))); // Outputs: 2019-08-14 02:00:00
```
Subtract minutes from date
```php
// Using server date-time
echo date('Y-m-d H:i:s', strtotime('-60 minutes')); // Outputs: 2019-08-26 10:48:23
//Using custom date-time
echo date('Y-m-d H:i:s', strtotime('-60 minutes', strtotime('2019-08-13 14:00:00'))); // Outputs: 2019-08-13 02:00:00
```
Add minutes to date
```php
// Using server date-time
echo date('Y-m-d H:i:s', strtotime('+60 minutes')); // Outputs: 2019-08-26 12:30:33
//Using custom date-time
echo date('Y-m-d H:i:s', strtotime('+60 minutes', strtotime('2019-08-13 14:00:00'))); // Outputs: 2019-08-13 13:00:00
```
Subtract seconds from date
```php
// Using server date-time
echo date('Y-m-d H:i:s', strtotime('-300 seconds')); // Outputs: 2019-08-26 11:40:33
//Using custom date-time
echo date('Y-m-d H:i:s', strtotime('-300 seconds', strtotime('2019-08-13 14:00:00'))); // Outputs: 2019-08-13 13:55:00
```
Add seconds to date
```php
// Using server date-time
echo date('Y-m-d H:i:s', strtotime('+60 seconds')); // Outputs: 2019-08-26 11:50:58
//Using custom date-time
echo date('Y-m-d H:i:s', strtotime('+60 seconds', strtotime('2019-08-13 14:00:00'))); // Outputs: 2019-08-13 14:05:00
```
Get the yesterdays date
```php
// Using server date-time
echo date('Y-m-d H:i:s', strtotime('yesterday')); // Outputs: 2019-08-25 00:00:00
//Using custom date-time
echo date('Y-m-d H:i:s', strtotime('yesterday', strtotime('2019-08-13 14:00:00'))); // Outputs: 2019-08-12 00:00:00
```
Get tomorrows date
```php
// Using server date-time
echo date('Y-m-d H:i:s', strtotime('tomorrow')); // Outputs: 2019-08-27 00:00:00
//Using custom date-time
echo date('Y-m-d H:i:s', strtotime('tomorrow', strtotime('2019-08-13 14:00:00'))); // Outputs: 2019-08-14 00:00:00
```
Get next date
Note: the next parameter works along with day, month and year.
```php
// Using server date-time
echo date('Y-m-d H:i:s', strtotime('next month')); // Outputs: 2019-09-26 20:40:43
//Using custom date-time
echo date('Y-m-d H:i:s', strtotime('next year', strtotime('2019-08-13 14:00:00'))); // Outputs: 2020-08-13 14:00:00
```
Get previous date
```php
// Using server date-time
echo date('Y-m-d H:i:s', strtotime('previous month')); // Outputs: 2019-07-26 20:40:43
//Using custom date-time
echo date('Y-m-d H:i:s', strtotime('previous year', strtotime('2019-08-13 14:00:00'))); // Outputs: 2018-08-13 14:00:00
```
Next day name
Note: short month name or full month name can be used, eg. Sat or Saturday ```php
echo date('Y-m-d H:i:s', strtotime('next saturday')); // Outputs: 2019-08-31 00:00:00
```
Last day name
Note: short month name or full month name can be used, eg. Sat or Saturday
php
echo date('Y-m-d H:i:s', strtotime('last saturday')); // Outputs: 2019-08-24 00:00:00
First day of month name
Note: short month name or full month name can be used, eg. Feb or February
php
echo date('Y-m-d H:i:s', strtotime('first day of February')); // Outputs: 2019-02-01 00:00:00
Last day of month name
Note: short month name or full month name can be used, eg. Feb or February
php
echo date('Y-m-d H:i:s', strtotime('last day of February')); // Outputs: 2019-02-28 00:00:00
Noon And Midnight Date
```php
echo date('Y-m-d H:i:s', strtotime('midnight')); // Outputs: 2019-08-26 00:00:00
echo date('Y-m-d H:i:s', strtotime('noon')); // Outputs: 2019-08-26 00:00:00
``` PDF Download Feel free to download this compacted cheat-sheet pdf. You can download the PDF Here
If you want to bookmark the original post, feel free with the following link - https://www.codewall.co.uk/php-date-strtotime-cheat-sheet/
Comments section