PHP Date and day difference function

Source: Internet
Author: User
Tags current time

Find the difference function of 22 date

<?php
$time 1 = "2008-6-15 11:49:59";//The first time
$time 2 = "2007-5-5 12:53:28";/The second time
$t 1 = strtotime ($time 1);
$t 2 = strtotime ($time 2);
$t = ABS ($t 1-$t 2);
$start = 0;
$string = "Two time difference:";
$y = Floor ($t 12/(3600*24*360));
if ($start | | $y)
{
$start = 1;
$t-= $y *3600*24*360;
$string. = $y. " Years ";
}
$m = Floor ($t 12/(3600*24*31));
if ($start | | $m)
{
$start = 1;
$t-= $m *3600*24*31;
$string. = $m. " Month ";
}
$d = Floor ($t 12/(3600*24));
if ($start | | $d)
{
$start = 1;
$t-= $d *3600*24;
$string. = $d. " Days ";
}
$h = Floor ($t 12/(3600));
if ($start | | $h)
{
$start = 1;
$t-= $h *3600;
$string. = $h. " When ";
}
$s = floor ($t 12/(60));
if ($start | | $s)
{
$start = 1;
$t-= $s *60;
$string. = $s. " Points ";
}
$string. = "{$t 12} seconds";
Echo $string;
?>

This is a function that asks for the difference of any time.

?
#作者: Xian le
#功能: To get a time difference between any and current time
function Querydays ($DATESTR) {
#格式化时间
$da =preg_split ("/(-| |:) /i ", $datestr);
$nowyear =date ("Y");
$nowmon =date ("n");
$nowday =date ("D");
$nowtimes =mktime (0,0,0, $nowmon, $nowday, $nowyear);
$pdtimes = Mktime (0,0,0, $nowmon, $nowday, $nowyear-1);
$bjtimes = Mktime (0,0,0, $da [1], $da [2], $da [0]);
#判断所给出的时间是不是在一年内
if ($bjtimes >= $pdtimes and $bjtimes <= $nowtimes) {
Return (Floor strftime ("%j", Mktime (0,0,0, $nowmon, $nowday, $nowyear)-mktime ($da [3], $da [4], $da [5], $da [1], $da [2],$ Da[0])));
}else{
$loop = $nowyear-$da [0];
$totaldays = (Floor (strftime ("%j", Mktime (0,0,0, $nowmon, $nowday, $nowyear)-mktime (0,0,0,1,1, $nowyear)));
for ($i =1; $i <= $loop; $i + +) {
For ($j =12 $j >=1; $j-) {
if ($da [0]== $nowyear-$i and $da [1]== $j) {
$days =mondays ($nowyear-$i, $j);
return $totaldays + + $days-$da [2];
Break
}else{
$days =mondays ($nowyear-$i, $j);
$totaldays + + $days;
}//end Else
}//end for
}//end for
}//end Else
}//end function
#取得月分的天数
Function Mondays ($year, $month) {
Switch ($month) {
Case "1":
Case "3":
Case "5":
Case "7":
Case "8":
Case "10":
Case "a": $days =31;break;
Case "4":
Case "6":
Case "9":
Case "One": $days =30;break;
Case "2":
if (Checkdate ($month, $year)) {
$days = 29;
}else{
$days = 28;
}//end Else
Break
}//end switch
return $days;
}//end function
$datestr = "2002-1-14 9:47:20";
echo querydays ($DATESTR);
?>

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.