php get time calculate time difference

Use php to get the timestamp of the current week, the first day and the last day of the month.

1. Get today's time frame:

<?php
$start = mktime(0,0,0,date("m"),date("d"),date("Y"));
$end = mktime(0,0,0,date("m"),date("d")+1,date("Y"));

2. Get the timestamp of the first/last day of the week

<?php
$year = date("Y");
$month = date("m");
$day = date('w');
$nowMonthDay = date("t");

$firstday = date('d') - $day;
if(substr($firstday,0,1) == "-"){
 $firstMonth = $month - 1;
 $lastMonthDay = date("t",$firstMonth);
 $firstday = $lastMonthDay - substr($firstday,1);
 $time_1 = strtotime($year."-".$firstMonth."-".$firstday);
}else{
 $time_1 = strtotime($year."-".$month."-".$firstday);
}
  
$lastday = date('d') + (7 - $day);
if($lastday > $nowMonthDay){
 $lastday = $lastday - $nowMonthDay;
 $lastMonth = $month + 1;
 $time_2 = strtotime($year."-".$lastMonth."-".$lastday);
}else{
 $time_2 = strtotime($year."-".$month."-".$lastday);
}

3. Get the timestamp of the first/last day of the month

<?php
$year = date("Y");
$month = date("m");
$allday = date("t");
$strat_time = strtotime($year."-".$month."-1");
$end_time = strtotime($year."-".$month."-".$allday);

Calculate time difference

$one = strtotime('2011-12-08 07:02:40');//Start time timestamp
$tow = strtotime('2011-12-25 00:00:00');//End time timestamp
$cle = $tow - $one; //get timestamp difference

$d = floor($cle/3600/24);
$h = floor(($cle%(3600*24))/3600); / /% remainder
$m = floor(($cle%(3600*24))600/60);
$s = floor(($cle%(3600*24))`);

echo "The difference between the two times is $d days $h hours $m minutes $s seconds"

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325353618&siteId=291194637