PHP gets time to calculate the difference

Source: Internet
Author: User

Get the time stamp for this week, the first day of the month, and the last day with PHP.

1, get today's time range:

2, get the timestamp of the first day/last day of the week

<?php$year = Date ("Y"), $month = Date ("M"), $day = Date (' W '), $nowMonthDay = Date ("T"), $firstday = Date (' d ')-$day; if (sub STR ($firstday, 0,1) = = "-") {$firstMonth = $month-1; $lastMonthDay = date ("T", $firstMonth); $firstday = $lastMonthDay-S Ubstr ($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 time stamp of the first day/last day of this 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; The time stamp difference value is obtained

$d = Floor ($cle/3600/24);
$h = Floor (($cle% (3600*24))/3600); % take-up
$m = Floor (($cle% (3600*24)) 600/60);
$s = floor (($cle% (3600*24));

echo "Two time difference $d days $h hours $m minutes $s seconds"

PHP gets time to calculate the difference

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.