The ultimate PHP date cheatsheet

by  
Share:
php date cheatsheet

Date and time is the most crucial aspect of any project. Whether it is a CRM, ERP, or even a mini address book, logging the date and time to the database is necessary. Hence, we have compiled the ultimate PHP date cheat sheet, which will suffice all your PHP date & time-related queries.

Get & Set time zone in PHP

It is extremely important to understand, failure to set a time zone before using the PHP date function can result in incorrect results.

To set time zone use this date_default_timezone_set() function. Find and set your time zone based on your region from their official site.

<?php
// output date and time of Universal Time Coordinated
date_default_timezone_set('UTC');
echo date("Y-m-d H:i:s");

// output date and time for Kolkata Region
date_default_timezone_set('Asia/Kolkata');
echo date("Y-m-d H:i:s");
?>

Get current date & time

To get the current date & time, use the PHP date function.

<?php
// current date 
echo date("Y-m-d");
// current time  
echo date("H:i:s");
// current time and time   
echo date("Y-m-d H:i:s");
?>

Another method of getting the current date & time is to use PHP DateTime class.

<?php 
$date = new DateTime();
// current date 
echo $date->format('Y-m-d');
// current time 
echo $date->format('H:i:s');
// current date and date 
echo $date->format('Y-m-d H:i:s');
?>

Get yesterday date

To get the yesterday date use this code echo date("Y-m-d",strtotime("yesterday"));

Get tomorrow date

To get the tomorrow date use this code echo date("Y-m-d",strtotime("tomorrow"));

Date time format parameter list

The following characters are recognized in the format parameter of the date function of the DateTime class. the list is been taken from their official site.

Character Description Example returned values
Day
d Day of the month, 2 digits with leading zeros 01 to 31
D A textual representation of a day, three letters Mon through Sun
j Day of the month without leading zeros 1 to 31
l (lowercase ‘L’) A full textual representation of the day of the week Sunday through Saturday
N ISO-8601 numeric representation of the day of the week 1 (for Monday) through 7 (for Sunday)
S English ordinal suffix for the day of the month, 2 characters st, nd, rd or
th. Works well with j
w Numeric representation of the day of the week 0 (for Sunday) through 6 (for Saturday)
z The day of the year (starting from 0) 0 through 365
Week
W ISO-8601 week number of year, weeks starting on Monday Example: 42 (the 42nd week in the year)
Month
F A full textual representation of a month, such as January or March January through December
m Numeric representation of a month, with leading zeros 01 through 12
M A short textual representation of a month, three letters Jan through Dec
n Numeric representation of a month, without leading zeros 1 through 12
t Number of days in the given month 28 through 31
Year
L Whether it’s a leap year 1 if it is a leap year, 0 otherwise.
o ISO-8601 week-numbering year. This has the same value as
Y, except that if the ISO week number
(W) belongs to the previous or next year, that year
is used instead.
Examples: 1999 or 2003
Y A full numeric representation of a year, 4 digits Examples: 1999 or 2003
y A two digit representation of a year Examples: 99 or 03
Time
a Lowercase Ante meridiem and Post meridiem am or pm
A Uppercase Ante meridiem and Post meridiem AM or PM
B Swatch Internet time 000 through 999
g 12-hour format of an hour without leading zeros 1 through 12
G 24-hour format of an hour without leading zeros 0 through 23
h 12-hour format of an hour with leading zeros 01 through 12
H 24-hour format of an hour with leading zeros 00 through 23
i Minutes with leading zeros 00 to 59
s Seconds with leading zeros 00 through 59
u Microseconds. Note that
date() will always generate
000000 since it takes an int
text-default, whereas DateTime::format() does
support microseconds if DateTime was
created with microseconds.
Example: 654321
v Milliseconds. Same note applies as for
u.
Example: 654
Timezone
e Timezone identifier Examples: UTC, GMT, Atlantic/Azores
I (capital i) Whether or not the date is in daylight saving time 1 if Daylight Saving Time, 0 otherwise.
O Difference to Greenwich time (GMT) without colon between hours and minutes Example: +0200
P Difference to Greenwich time (GMT) with colon between hours and minutes Example: +02:00
p The same as P, but returns Z instead of +00:00 Example: +02:00
T Timezone abbreviation Examples: EST, MDT
Z Timezone offset in seconds. The offset for timezones west of UTC is always
negative, and for those east of UTC is always positive.
-43200 through 50400
Full Date/Time
c ISO 8601 date 2004-02-12T15:19:21+00:00
r » RFC 2822 formatted date Example: Thu, 21 Dec 2000 16:01:07 +0200
U Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT) See also time()

Change PHP date format mm/dd/yyyy to yyyy-mm-dd

There are several ways to accomplish this, I am listing two approaches here.

<?php
// Method-1
$date = "01/31/2021"; 
$res = explode("/", $date); 
$changedDate = $res[2]."-".$res[0]."-".$res[1]; 
echo $changedDate; //prints 2021-01-31
?>

<?php
// Method-2
$date = "01/31/2021"; 
$date = DateTime::createFromFormat("m/d/Y" , $date); 
echo $date->format('Y-m-d');
?>

Change PHP date format d/m/y to y-m-d

Similar to the problem above, we solve it in the same way. All you need is to set the format correctly.

<?php 
// Method-1 
$date = "31/01/2021";
$res = explode("/", $date);
$changedDate = $res[2]."-".$res[1]."-".$res[0]; 
echo $changedDate; //prints 2021-01-31
?> 

<?php 
// Method-2 
$date = "31/01/2021"; 
$date = DateTime::createFromFormat("d/m/Y" , $date); 
echo $date->format('Y-m-d');
?>

Get last month date range

A simple solution to get the first day of last month, use echo date('Y-m-d',strtotime('first day of last month')); and to get last day of last month, use echo date('Y-m-d',strtotime('last day of last month'));

Alternatively, this one-liner code, to get the first and last day of last month, can be used to achieve the same result. echo date('Y-m-01 - Y-m-t', strtotime('previous month'));

If you want a customized solution that suits your needs, use the custom function below.

<?php

/*
* defining a custom function where you can set specific date or it will take the current date
*/
function get_last_month_range($day="", $month="", $year ="") { 
  $tday = ($day == "" ) ? "01" : $day;
  $tmonth = ($month == "" ) ? date("m") : $month;
  $tyear = ($year == "" ) ? date("Y") : $year;

  $month_sd = date("Y-m-d", strtotime($tmonth.'/'.$tday.'/'.$tyear.' 00:00:00 -2 month'));	
  $month_ed = date("Y-m-d", strtotime('-1 second',strtotime('-1 month',strtotime($tmonth.'/'.$tday.'/'.$tyear.' 00:00:00'))));

  return "Last Month range from $month_sd to $month_ed ";
}

// usage of this function

// if no date is provided then current month is taken
echo get_last_month_range();

echo "<br>";
// prints the last month date range from given date
echo get_last_month_range(15, 10, 2021);

?>

Get next month date range

To get the first day of next month, use echo date('Y-m-d',strtotime('first day of next month')); and to get last day of next month, use echo date('Y-m-d',strtotime('last day of next month'));

You can also use this one-liner code to get the first and last day of next month echo date('Y-m-01 - Y-m-t', strtotime('next month'));

Similarly, for customized solution, use the custom function below.

<?php
/*
* defining a custom function where you can set specific date or it will take the current date
*/
function get_next_month_range($day="", $month="", $year ="") { 
  $tday = ($day == "" ) ? "01" : $day;
  $tmonth = ($month == "" ) ? date("m") : $month;
  $tyear = ($year == "" ) ? date("Y") : $year;

  $month_sd = date("Y-m-d", strtotime($tmonth.'/'.$tday.'/'.$tyear.' 00:00:00 +1 month'));
  $month_ed = date("Y-m-d", strtotime('-1 second',strtotime('+1 month',strtotime($tmonth.'/'.$tday.'/'.$tyear.' 00:00:00 +1 month'))));

  return "Next Month range from $month_sd to $month_ed ";
}

// usage of this function

// if no date is provided then current month is taken
echo get_next_month_range();

echo "<br>";
// prints the next month date range from the given date
echo get_next_month_range(15, 10, 2021);
?>

Get current month date range

To get the first day of current month, use echo date('Y-m-01'); and to get last day of current month, just use echo date('Y-m-t');

Alternative solution to get the first and last day of the current month is to use echo date('Y-m-01 - Y-m-t');

For customized solution, use the custom function below.

<?php

/*
* defining a custom function where you can set specific date or it will take the current date
*/
function get_current_month_range($month="", $year ="") { 
  $tfday = "01";
  $tmonth = ($month == "" ) ? date("m") : $month;
  $tyear = ($year == "" ) ? date("Y") : $year;

  $month_sd = date("Y-m-d", strtotime($tmonth.'/'.$tfday.'/'.$tyear.' 00:00:00')); 
  $month_ed = date("Y-m-t", strtotime($tmonth.'/'.$tfday.'/'.$tyear.' 00:00:00')); 

  return "Month range from $month_sd to $month_ed ";
}

// usage of this function

// if no date is provided then current month is taken
echo get_current_month_range();

echo "<br>";
// prints the current month date range from the given date
echo get_current_month_range(02, 2021);
?>

Get next week date range

If you just want the date, one week from today, use echo date("Y-m-d", strtotime("+1 week"));

But, if you want to get next week’s date range (start week date & end week date) week starting day will be Monday, use the code given below.

<?php
$monday = strtotime("next monday");
$monday = (date('W', $monday)==date('W') ) ? $monday-7*86400 : $monday;

$sunday = strtotime(date("Y-m-d",$monday)." +6 days");
$this_week_sd = date("Y-m-d",$monday);
$this_week_ed = date("Y-m-d",$sunday);

echo "Next week range from $this_week_sd to $this_week_ed ";
?>

Get last week date range

If you want the date of last week from current day, use echo date("Y-m-d", strtotime("-1 week"));

But, if you want to get last week date range (start week date & end week date) week starting day will be Monday, use the code given below.

<?php
$monday = 	strtotime("last monday");
$monday = (date('W', $monday)==date('W') ) ? $monday-7*86400 : $monday;

$sunday = strtotime(date("Y-m-d",$monday)." +6 days");
$this_week_sd = date("Y-m-d",$monday);
$this_week_ed = date("Y-m-d",$sunday);

echo "Last week range from $this_week_sd to $this_week_ed ";
?>

Get current week date range

To get the current week date range (start week date & end week date) starting day will be Monday, use the code given below.

<?php
$monday = strtotime("last monday");
$monday = date('w', $monday)==date('w') ? $monday+7*86400 : $monday;

$sunday = strtotime(date("Y-m-d",$monday)." +6 days");
$this_week_sd = date("Y-m-d",$monday);
$this_week_ed = date("Y-m-d",$sunday);

echo "Current week range from $this_week_sd to $this_week_ed ";
?>

Find the number of days in any month.

If you want to find the number of days for the current month, use echo date("t");

In another case, let’s say you want to get the number of days for February 2021. Simply use echo date("t", strtotime("2021-02-01"));

Get the latest updates directly in your inbox for FREE: Subscribe here

Share your feedback / let me know your doubts regarding this tutorial in the comment box given below.

Share:
Hasan

Shahrukh Khan (Hasan)

A software engineer who's a die-hard coder, blogger, dreamer and mentor with years of expertise in web development. Know more...

Related Posts

hitman on

Please how can I get popular post of a week ?

Reply
Hasan on

can you give me more details.

Reply

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.