PHP

Insert timing examples, esp. for testing code.

year

date('Y') 4 digit year : 2017

date('y') 2 digit year : 17

month

date('M') short name of month : Sep

date('F') full name of month : September

date('m') number of month (01..12) : 09

date('n') numeric month (1..12) : 9

date('t') number of days in current month: 30

week

date('W') week in year : 38

days

date('D') short name of day : Sun

date('l') full name of day : Sunday

date('w') numeric day in week (0..6) : 0

date('d') day in month : 24

date('S') 2 char ordinal suffix for day : th

date('z') numeric day in year (0..364): 266

time

date('H:i:s') 24-hour time : 03:10:42

date('U') UNIX timestamp : 1506215442

getdate()

[seconds] 42

[minutes] 10

[hours] 3

[mday] 24

[wday] 0

[mon] 9

[year] 2017

[yday] 266

[weekday] Sunday

[month] September

[0] 1506215442 (UNIX timestamp)

date('Y-m-d') 2017-09-24

date('Y-m-d H:i:s') 2017-09-24 03:10:42

date('m-d-y') 09-24-17

date('M. d, Y') Sep. 24, 2017

date('l, \t\h\e jS \o\f F') Sunday, the 24th of September
NOTE — single quotes prevent PHP from turning \t and \f into TAB and FF

date & getdate can format other dates
IF they're converted to timestamps

mktime( h, min, sec, mon, d, yr )
creates a timestamp — a date in seconds since the epoch, 1-1-1970

find the date, 18 days from now

Create timestamp for 18 days from now.

$plus18d = mktime( 0, 0, 0, date('m'), date('d') +18, date('Y') );

date('l, M-d-Y',$plus18d) Thursday, Oct-12-2017

getdate( $plus18d ) array for 10-12-2017

find day of week 1 year ago

$minus1yr = mktime( 0, 0, 0, date('m'), date('d'), date('Y')-1 )

date('l, M-d-Y',$minus1yr) Saturday, Sep-24-2016

week boundaries

Create timestamp for current Sunday (day in month (24) - numeric weekday (0)) :

$curr_sunday= mktime( 0, 0, 0, date('m'), date('d')-date('w'), date('Y') );

Create timestamp based on current Sunday plus 7 days :

$next_sunday = mktime( 0, 0, 0, date('m',$curr_sunday), date('d',$curr_sunday)+7, date('Y',$curr_sunday) )

DAY-LIGHT SAVINGS / STANDARD TIME!

Don't calculate a week as 604800 seconds — twice a year it will be wrong.

BAD: $next_sunday = $curr_sunday + 604800; (use mktime() above)