Home >Backend Development >PHP Tutorial >Example of php timestamp conversion_PHP tutorial
The following example produces the result:
//Date conversion of yesterday, today and tomorrow
//($startstr Today’s start timestamp)
//Return 0:00 and 23:59:59 of (yesterday, today and tomorrow)
function alldaytostr($startstr) {
$oneday_count = 3600 * 24; //How many seconds are there in a day
//Tomorrow
$tomorrow_s = $startstr + $oneday_count; //Start tomorrow
$tomorrow_e = $tomorrow_s + $oneday_count - 1; //Ends tomorrow
//Yesterday
$yesterday_s = $startstr - $oneday_count; //Starts yesterday
$yesterday_e = $startstr - 1; // Ended yesterday
//Ended today
$today_e = $tomorrow_s - 1;
//Yesterday, today and tomorrow at 0:00 and 23:59:59 of the day are combined into an array
$allday_array = array ('yesterday' => array($yesterday_s, $yesterday_e),
'today' => array($startstr, $today_e),
'tomorrow' => array($tomorrow_s, $tomorrow_e ));
return $allday_array;
}
//Start time of the day
$btime = date('Y-m-d'.'00:00:00',time());
//Convert to "start" timestamp
$btimestr = strtotime($btime);
var_dump(alldaytostr($btimestr));
?>