You are on page 1of 2

PHP › Datetime

Datetime ref ›› date('d-m-Y', mktime( 0, 0, 0, date('n') - 1,


date('j'), date('y')))

date_default_timezone_set(string)
getdate (timestamp = time())
Change a time zone within php script
Return time and date information in an array
date_default_timezone_set ('Asia/Jakarta');
$today = getdate();
To change permanently, modify php configuration in the // Array ( [seconds] => 0 [minutes] => 50
php.ini file [hours] => 23 [mday] => 21 [wday] => 2 [mon] = >
3 [year] => 2017 [yday] => 79 [weekday] =>
date.timezone = Asia/Jakarta Tuesday [month] => March [0] => 1490136600 )

timestamp date_create (string,object DateTimeZone=null)


timestamp is the term for a time in the UNIX format, that Create DateTime Object
is the number of seconds from the epoch time (01-01-
$now = date_create();
1970 00:00:00), timestamp 50 means 01-01-1970 $date = date_format($now, 'l j, Y H:i:s'); //
00:00:50. To create timestamp, use time(), strtotime() and March 22, 2017 12:27:23
mktime() function
With DateTimeZone object:

date (format, timestamp = time()) $now = date_create('now',


new DateTimeZone('Asia/Jakarta'));
Display local time with a particular format $date = date_format($now, 'l j, Y H:i:s'); //
March 22, 2017 18:27:23 -> +7 hour
$date = date ('l j, Y H:i:s'); // Now: March 22,
2017 05:31:27
date_diff (datetime1, datetime2)
$date = date ('l j, Y', strtotime('2017-05-
01')); // May 01, 2017
Calculates the time difference between two dates, the
arguments must be a DateInterval object (can be resulted
time()
from date_create() function)
The time() function has no argument, it return the current $start = date_create('1988-08-10');
time in the UNIX format (GMT + 0) $end = date_create(); // now
$diff = date_diff( $start, $end );
time(); // 1490135985
date('l j, Y H:i:s', time()); March 22, 2017 echo 'The difference: '.$diff->y.' years, '.
05:32:27 $diff->m.' months, '.$diff->d.' days, ' .
date('l j, Y H:i:s', time() + 60); March 22, $diff->h.' hours, '.$diff->i.' minutes, '.
2017 05:33:27 $diff->s.' seconds, ';
// The difference: 28 years, 5 months, 9 days,
strtotime (str) 13 hours, 7 minutes, 7 seconds

Return date in UNIX (GMT + 0) format from particular echo 'Day difference : ' . $diff->days;
string. For string format, see relative format section // Day difference: 10398
strtotime('now'); // 1490135639 Format the output:
date('d-m-Y H:i:s', strtotime('now')); // 22-03-
2017 05:35:04 $diff->format('Your age is %Y years and %d
date('d-m-Y H:i:s', strtotime('+1 day')); // 23- days'); // Your age is 29 years 9 days.
03-2017 05:35:04
Format:
date('d-m-Y H:i:s', strtotime('-24 hours')); //
21-03-2017 05:35:04  %Y for 4 digit year and %y for 2 digit year,
 %M for month with leading 0 and %m for month
mktime(hour, min, sec, month, day, year) without leading 0,
 %D for day with leading zero and %d for day without
Return date in UNIX (GMT + 0) format leading zero
date('m/d/Y', mktime(0,0,0,1,32,2017)); //  %a for total days
02/01/2017

©2017 • WebDevZoom.com
Character for format parameter string ref ›› Relative Format ref ››

Used Symbol
-- Day ---

d 01 to 31 Description Format

j 1 to 31 ordinal 'first' | 'second' | 'third' | 'fourth' | 'fifth' |


'sixth' | 'seventh' | 'eighth' | 'ninth' |
Day in week
'tenth' | 'eleventh' | 'twelfth' | 'next' | 'last'
D Mon through Sun | 'previous' | 'this'
l Sunday through Saturday daytext 'weekday' | 'weekdays'
N 1 (for Monday) through 7 (for Saturday) reltext 'next' | 'last' | 'previous' | 'this'
w 0 (for Sunday) through 6 (for Saturday) unit (('sec' | 'second' | 'min' | 'minute' | 'hour' |
-- Month -- 'day' | 'fortnight' | 'forthnight' | 'month' |
'year') 's'?) | 'weeks' | daytext
m 01 through 12

n 1 through 12 Day-based Notations

F January through December Format Examples


M Jan through Dec 'yesterday' "yesterday 14:00"

t Number of days in the given month (28 'midnight'


through 31)
'today'
-- Year --
'now'
Y Four digits year (e.g. 2017)
'first day of' "first day of April 2017"
y Two digits year (e.g. 17)
'last day of' "last day of next month"
L Whether it’s a leap year (1 if it is a leap
ordinal [space] dayname "first sat of April 2017"
year, 0 otherwise)
[space] of'
-- Time -- 'last' [space] dayname "last sat of April 2017"
a am or pm [space] 'of''

A AM or PM

Hour

g 1 through 12

h 01 through 12

G 0 through 23

H 00 through 23

Minutes and Seconds

i Minutes (00 to 59)

s Seconds (00 to 59)

©2017 • WebDevZoom.com

You might also like