PHP Find time ago between two times
/** * Formats a timestamp nicely with an adaptive "x units of time ago" message. * Only handles past dates. * @return string Nicely-formatted message for the timestamp. * @param $time Output of strtotime() on your choice of timestamp. */ function ago($time) { $time = strtotime($time); $delta = time() - $time; if ($delta < 60) { return 'less than a minute ago.'; } else if ($delta < 120) { return 'about a minute ago.'; } else if ($delta < (45 * 60)) { return floor($delta / 60) . ' minutes ago.'; } else if ($delta < (90 * 60)) { return 'about an hour ago.'; } else if ($delta < (24 * 60 * 60)) { return 'about ' . floor($delta / 3600) . ' hour(s) ago.'; } else if ($delta < (48 * 60 * 60)) { return '1 day ago.'; } else { return floor($delta / 86400) . ' days ago.'; } }
Number Days Any Month
<?php$lastday = mktime(0, 0, 0, 3, 0, 2006); printf("There are %d days in February, 2006.", date("t",$lastday));?>
Number Days Current Month
<?phpprintf("There are %d days in %s.", date("t"), date("F"));?>
Offsets From Utc
$pc_timezones = array( 'GMT' => 0, // Greenwich Mean 'UTC' => 0, // Universal (Coordinated) 'WET' => 0, // Western European 'WAT' => -1*3600, // West Africa 'AT' => -2*3600, // Azores 'NFT' => -3*3600-1800, // Newfoundland 'AST' => -4*3600, // Atlantic Standard 'EST' => -5*3600, // Eastern Standard 'CST' => -6*3600, // Central Standard 'MST' => -7*3600, // Mountain Standard 'PST' => -8*3600, // Pacific Standard 'YST' => -9*3600, // Yukon Standard 'HST' => -10*3600, // Hawaii Standard 'CAT' => -10*3600, // Central Alaska 'AHST' => -10*3600, // Alaska-Hawaii Standard 'NT' => -11*3600, // Nome 'IDLW' => -12*3600, // International Date Line West 'CET' => +1*3600, // Central European 'MET' => +1*3600, // Middle European 'MEWT' => +1*3600, // Middle European Winter 'SWT' => +1*3600, // Swedish Winter 'FWT' => +1*3600, // French Winter 'EET' => +2*3600, // Eastern Europe, USSR Zone 1 'BT' => +3*3600, // Baghdad, USSR Zone 2 'IT' => +3*3600+1800, // Iran 'ZP4' => +4*3600, // USSR Zone 3 'ZP5' => +5*3600, // USSR Zone 4 'IST' => +5*3600+1800, // Indian Standard 'ZP6' => +6*3600, // USSR Zone 5 'SST' => +7*3600, // South Sumatra, USSR Zone 6 'WAST' => +7*3600, // West Australian Standard 'JT' => +7*3600+1800, // Java 'CCT' => +8*3600, // China Coast, USSR Zone 7 'JST' => +9*3600, // Japan Standard, USSR Zone 8 'CAST' => +9*3600+1800, // Central Australian Standard 'EAST' => +10*3600, // Eastern Australian Standard 'GST' => +10*3600, // Guam Standard, USSR Zone 9 'NZT' => +12*3600, // New Zealand 'NZST' => +12*3600, // New Zealand Standard 'IDLE' => +12*3600 // International Date Line East );
Outputs The Date In The Format Of 31St Of August 2005
print date("jS of F Y");
Parsing A Date With Substr()
$date = '2010-12-03 05:12:56';$date_parts[0] = substr($date,0,4); $date_parts[1] = substr($date,5,2); $date_parts[2] = substr($date,8,2); $date_parts[3] = substr($date,11,2); $date_parts[4] = substr($date,14,2); $date_parts[5] = substr($date,17,2); ?>
Printing A Formatted Date String With Strftime()
<? print strftime('%m/%d/%y'); ?>
Printing A Formatted Time String With Other Text
<? print 'strftime() says: '; print strftime('Today is %m/%d/%y and the time is %I:%M:%S'); print "\n"; print 'date() says: '; print 'Today is ' . date('m/d/y') . ' and the time is ' . date('h:i:s'); ?>
Printing A Greeting Or Printing A Form
<?php if ($_POST['user']) { print "Hello, "; print $_POST['user']; print "!"; } else { print <<<_HTML_ <form method="post" action="$_SERVER[PHP_SELF]"> Your Name: <input type="text" name="user"> <br/> <input type="submit" value="Say Hello"> </form> _HTML_; } ?>
Return Array From Getdate( )
Key Value seconds Seconds minutes Minutes hours Hours mday Day of the month wday Day of the week, numeric (Sunday is 0, Saturday is 6) mon Month, numeric year Year, numeric (4 digits) yday Day of the year, numeric (e.g., 299) weekday Day of the week, textual, full (e.g., "Friday") month Month, textual, full (e.g., "January") 0 Seconds since epoch (what time( ) returns)
Return Array From Localtime( )
Numeric position Key Value 0 tm_sec Second 1 tm_min Minutes 2 tm_hour Hour 3 tm_mday Day of the month 4 tm_mon Month of the year (January is 0) 5 tm_year Years since 1900 6 tm_wday Day of the week (Sunday is 0) 7 tm_yday Day of the year 8 tm_isdst Is daylight savings time in effect?
Setting Time Zones And Gmt/Utc
<?php $ts = time(); echo date('r', $ts) . "<br />\n"; echo date('r', $ts) . "<br />\n"; echo strftime("%D %T %Z", $ts) . "<br />\n"; ?>
Simple Time Zone Calculation
<?php$time_parts = localtime();$california_time_parts = localtime(time() - 3 * 3600); ?>
Skip Ahead One Day To The Tuesday After The First Monday
<? $monday = strtotime('Monday', $november);$election_day = strtotime('+1 day', $monday);print strftime('Election day is %A, %B %d, %Y', $election_day); ?>
Strftime.Php
<?php setlocale(LC_ALL, "it_IT"); $tickets = 2; $departure_time = 1238837700; $return_time = 1239457800; $cost = 1350.99; ?> <?php echo $tickets; ?><br /> <?php echo strftime("%d %B, %Y", $departure_time); ?><br /> <?php echo strftime("%d %B, %Y", $return_time); ?><br />
String Date ( String Date_Format [, Int Timestamp] )
Format characters for use in date( )Format character Description Example a Lowercase am/pm am or pm A Uppercase am/pm AM or PM B Swatch Internet Time 000 to 999 c ISO 8601 date, time, and time zone 2004-06-18T09:26:55+01:00 d 2-digit day of month, leading zeros 01 to 31 D Day string, three letters Mon, Thu, Sat F Month string, full January, August g 12-hour clock hour, no leading zeros 1 to 12 G 24-hour clock hour, no leading zeros 0 to 23 h 12-hour clock hour, leading zeros 01 to 12 H 24-hour clock hour, leading zeros 00 to 23 i Minutes with leading zeros 00 to 59 I Is daylight savings time active? 1 if yes, 0 if no j Day of month, no leading zeros 1 to 31 l Day string, full Monday, Saturday L Is it a leap year? 1 if yes, 0 if no m Numeric month, leading zeros 01 to 12 M Short month string Jan, Aug n Numeric month, no leading zeros 1 to 12 O Difference from GMT 200 r RFC-822 formatted date Sat, 22 Dec 1979 17:30 +0000 s Seconds, with leading zeros 00 to 59 S English ordinal suffix for day number st, nd, rd, or th t Number of days in month 28 to 31 T Time zone for server GMT, CET, EST U Unix Timestamp 1056150334 w Numeric day of week 0 (Sunday), 6 (Saturday) W ISO-8601 week number of year 30 (30th week of the year) y Two-digit representation of year 97, 02 Y Four-digit representation of year 1997, 2002 z Day of year 0 to 366 Z Time zone offset in seconds -43200 to 43200
The Associative Array Returned By Getdate()
Key Description Example seconds Seconds past the minute (0�59) 28 minutes Minutes past the hour (0�59) 7 hours Hours of the day (0�23) 12 mday Day of the month (1�31) 20 wday Day of the week (0�6) 4 mon Month of the year (1�12) 1 year Year (four digits) 2004 Yday Day of year (0�365) 19 weekday Day of the week (name) Thursday month Month of the year (name) January 0 Timestamp 948370048
Timing With Microtime()
<?php $start = microtime(true); for ($i = 0; $i < 1000; $i++) { preg_match('/age=\d+/',$_SERVER['QUERY_STRING']); } $end = microtime(true); $elapsed = $end - $start;
To Obtain A Number Corresponding To The Day Of The Week, Use Getdate() Instead:
<?php $ts = strtotime('04 Jul 2007'); $gd = getdate($ts); $day = $gd["wday"]; ?>
Use Mktime To Create A Time
<? $stamp = mktime(19,45,0,10,20,2004); print strftime('Today is day %d of %B and day %j of the year %Y. The time is %I:%M %p (also known as %H:%M).', $stamp); ?>
