Fun With Dates

  • php

Working with dates in PHP can get cumbersome. Especially when you need to calculate a date occurrence in the past or future. These examples should help get those dates, and output them in a human readable format.

PHP Date Formatting

Y
4 digit year (2018)
y
2 digit year (18)
F
Long month (January)
M
Short month (Jan)
m
Month two digits (01 to 12)
n
Month (1 to 12)
D
Short day name (Mon)
l
Long day name (Monday) (lowercase L)
N
numeric representation of the day of the week – 1 (for Monday) through 7 (for Sunday)
w
Numeric representation of the day of the week – 0 (for Sunday) through 6 (for Saturday)
d
Day two digits (01 to 31)
j
Day (1 to 31)
h
12 Hour two digits (01 to 12)
g
12 Hour (1 to 12)
H
24 Hour two digits (00 to 23)
G
24 Hour (0 to 23)
i
Minutes two digits (00 to 59)
s
Seconds two digits (00 to 59)
w
Day of week (0 to 6)
z
Day of year (0 to 365)
W
Week of year (1 to 53)
t
Days in month (28 to 31)
a
am or pm
A
AM or PM
B
Swatch Internet Time (000 to 999)
S
Ordinal Suffix (st, nd, rd, th)
T
Timezone of machine (GMT)
Z
Timezone offset (seconds)
O
GMT offset (hours) (+0200)
I
Daylight saving (1 or 0)
L
Leap year (1 or 0)
U
Seconds since Epoch
c
ISO 8601 (PHP 5) (2008-­07-­31T­18:­30:­13+­01:00)
r
RFC 2822 (Thu, 31 Jul 2008 18:30:13 +0100)

Get next Week


// Get next week
echo date("l, M d Y", strtotime ("+1 week"));
// Result is: Tuesday, May 15 2018

Get next Monday


//Get next Monday
echo date("l, M d Y", strtotime("next monday"));
//Result is: Monday, May 14 2018

Get the first day of a given month


//Get the first Wednesday of December, 2015
echo date("l, M d, Y", strtotime("first wednesday of 2018-05"));
//Result is: Wednesday, May 02, 2018</span>

Get next Friday


//Get next Friday
echo date("l, M d, Y", strtotime("last friday"));
//Result is: Friday, May 04, 2018

Get the first day of next month


//Get the first day of next month
echo date("l, M d, Y", strtotime("first day of next month"));
//Result is: Friday, Jun 01, 2018

Get the first Monday in January


//Get the first Monday in January
echo date("l, M d, Y", strtotime("first monday of 2018-01"));
//Result is: Monday, Jan 01, 2018

First Monday of next month


//Get the first Monday of next month
echo date("l, M d, Y", strtotime("first monday of next month"));
//Result is: Monday, Jun 04, 2018

Get the last day of this month


//Get the last day of this / current month
echo date("l, M d, Y", strtotime("last day of this month"));
//Result is: Thursday, May 31, 2018

Get the second Friday of last month


//Get the second Friday of last month
echo date("l, M d, Y", strtotime("second friday of last month"));
//Result is: Friday, Apr 13, 2018

Get the last Friday of this month


//Get the last Friday of this month
echo date("l, M d, Y", strtotime("last friday of this month"));
//Result is: Friday, May 25, 2018

Close Menu