Re: the function of to calculate the diff of date
| From: | Gabor Hojtsy | Date: | Tue, 19 Aug 2003 07:09:36 +0000 |
| Subject: | Re: the function of to calculate the diff of date | ||
| References: | 1 | Groups: | php.mirrors |
| Request: | Send a blank email to php-mirrors+get-20495@lists.php.net to get a copy of this message | ||
I suggest you see http://php.net/strtotime
Goba
LeeYeeKang 李藝康 wrote:
> hi, i am Yeekang ,i used PHP in web programming. I think PHP isn't eazy to
> calculate the diff of day (ex how many day between 2003/08/15 and 2003/09/21),
> so i write this function and wish it can help others handles problems
> efficiently.
>
>
> string datediff(DATE $date,STRING $parameter,CHAR $split)
> $date is date format which like 2003/03/16 YYYY/MM/DD
> $parameter can be Date format or int,
> if $parameter's type is Date ,function will calucute the diff between with
> $date
> else $date will increase/decrease the $parameter
> $split is the mark to split the DATE, i think it may be useless @_@
>
> YeeKang
>
> <?
> function datediff($date,$parameter,$split="0"){
> //setting the pattern for split use
> if($split!="0") //if the pattern have set by user
> $pattern = $split;
> else //found out the character which use at $date
> $pattern = substr(ereg_replace("[0-9]","",$date),0,1);
>
> $date1 = explode($pattern,$date); //split the date => year,month,day
>
> if(!checkdate($date1[1],$date1[2],$date1[0])) //check the date format
> return FALSE;
>
> if(is_numeric($parameter)){ //if you want to increase or decrease
> $timestamp = mktime(0,0,0,$date1[1],$date1[2]+$parameter,$date1[0]);
>
> return date("Y-m-d",$timestamp); //return the date after
> increase/decrease
> }else{ //just found out the diff of the date
> if($split=="0")
> $pattern = substr(ereg_replace("[0-9]","",$parameter),0,1);
>
> $date2 = explode($pattern,$parameter); //split the parameter =>
> year,month,day
>
> if(!checkdate($date2[1],$date2[2],$date2[0])) //check the date format
> return FALSE;
>
> $timestamp1 = mktime(0,0,0,$date1[1],$date1[2],$date1[0]);
> //translate to timestamp
> $timestamp2 = mktime(0,0,0,$date2[1],$date2[2],$date2[0]);
> $diff = ($timestamp1 > $timestamp2)?($timestamp1 -
> $timestamp2):($timestamp2 - $timestamp1);
>
> return $diff/86400; //return the diff of the date
> }
> }
> echo "Today : ".date("Y-m-d")."<br>";
> echo "datediff(date(\"Y-m-d\"),4) : ".datediff(date("Y-m-d"),4);
> echo "<br>";
> echo "datediff(date(\"Y-m-d\"),-4) :
> ".datediff(date("Y-m-d"),-4);
> echo "<br>";
> echo "datediff(date(\"Y-m-d\"),\"2003-02-21\") :
> ".datediff(date("Y-m-d"),"2003-02-21");
> echo "<br>";
> echo "datediff(date(\"Y@m@d\"),\"2003@08@21\",\"@\") :
> ".datediff(date("Y@m@d"),"2003@08@21","@");
> echo "<br>";
> echo "datediff(date(\"Y@m@d\"),\"2003$08$21\") :
> ".datediff(date("Y@m@d"),"2003$08$21");
> ?>
>
> Today : 2003-08-18
> datediff(date("Y-m-d"),4) : 2003-08-22
> datediff(date("Y-m-d"),-4) : 2003-08-14
> datediff(date("Y-m-d"),"2003-02-21") : 178
> datediff(date("Y@m@d"),"2003@08@21","@") : 3
> datediff(date("Y@m@d"),"2003$08$21") : 3
>
>
>
> -------------------------------------------------
> This mail sent through NCTU WebMail System:
> https://webmail.nctu.edu.tw
>