Before an instance is explained, introduce several core functions: the
mktime function
mktime () function returns a Unix timestamp for a date. The
argument always represents the GMT date, so IS_DST has no effect on the result. The
parameter can be left-to-right, and the empty argument is set to the appropriate current GMT value.
Syntax: mktime (HOUR,MINUTE,SECOND,MONTH,DAY,YEAR,IS_DST)
parameters description
Hour Optional. Set hours.
minute Optional. Specify minutes.
second Optional. Specified seconds.
month Optional. A month that is specified in numbers.
day Optional. Stipulated day.
year Optional. Set year. On some systems, the legal value is between 1901-2038. However, there is no such limit in PHP 5.
is_dst Optional. If time is in daylight saving time (DST), set to 1, otherwise set to 0, if unknown, set to-1.
Since 5.1.0, IS_DST parameters have been discarded. Therefore, you should use the new time zone processing attributes.  &NBSP
Example: the Mktime () function is useful for date operations and validation. It automatically corrects the input:
<?php
Echo (Date ("M-d-y", Mktime (0,0,0,12,36,2001));
Echo (Date ("M-d-y", Mktime (0,0,0,14,1,2001));
Echo (Date ("M-d-y", Mktime (0,0,0,1,1,2001));
Echo (Date ("M-d-y", Mktime (0,0,0,1,1,99));
? >
Output:
jan-05-2002
feb-01-2002
jan-01-2001
jan-01-1999
Strtotime function
the Strtotime () function resolves the date-time description of any English text to a Unix timestamp.
Syntax:strtotime (time,now)
Parameter Description
time to specify the string to parse.
Now is the time stamp used to calculate the return value. If this argument is omitted, the current time is used.
After one week: Strtotime ("+1 Week");
A week ago: Strtotime ("-1 week");
January after: Strtotime ("+1 months");
After one day: Strtotime ("+1 days");
30 seconds later Strtotime ("+30 seconds");
20 minutes later Strtotime ("+20 minutes");
12 hours later Strtotime ("+12 hours");
Date function
The date () function formats a local time/date.
Grammar
Date (Format,timestamp)
Date_default_timezone_set function
The Date_default_timezone_set () function sets the default time zone for all date/time functions in the script.
Date_default_timezone_set (TimeZone)
instance
The first case is that there is no database, just get the date value of the comparison, it must be fully used in PHP time and date function, as follows:
For example, to calculate the number of days from 2015-9-5 to 2015-9-18:
<?php
$startdate =strtotime ("2015-9-5");
$enddate =strtotime ("2015-9-18"); The PHP time and date function above has turned the date into a timestamp, or a second. So as long as the two values subtract, and then the second into the day can be, relatively simple, as follows:
$days =round (($enddate-$startdate)/3600/24);
Echo $days; Days to get the day;
? >
The second kind of child's growth
?
Date_default_timezone_set (' Asia/shanghai '); The above sentence is set time zone, actually not set also line, but Zde debug when will have a hint, say what unsafe function ...
Add it up. echo Date (' y-m-d h:i:s '). ' Today is '. Date (' Y '). ' The first '. Date (' W ') of the year. '
Weeks ';
$stime = ' 2005-11-03 10:08 '; echo "<br/><br/>*** since birth (<font color=blue> $stime </font>) ...
:<br/><br/> "; echo "Today is the first <font color=red><b>". LNBSP (Daysofnow ($stime), 3). "
</b></font> Day <br/> "; echo "Today is the first <font color=red><b>". LNBSP (Weeksofnow ($stime), 3). "
</b></font> Week <br/> "; echo "Today is the first <font color=red><b>". LNBSP (Monthsofnow ($stime), 3). "
</b></font> month <br/> "; echo "Today is the first <font color=red><b>". LNBSP (Yearsofnow ($stime), 3). "
</b></font> year <br/> "; /* $output =sprintf ("Today is <font color=red><b>%03d</b></font> Day <br/> today is < font color= Red><b>%03d</b></font> Week <br/> today is < font color=red><b>%03d</b></ Font> a month <br/> Today is the first < font color=red><b>%03d</b></font> <br/& gt; ", Daysofnow ($stime),
Weeksofnow ($stime), Monthsofnow ($stime), Yearsofnow ($stime));
Echo $output;
* * Function Weeksofnow ($stime) {$ftime =strtotime ($stime);
$fweeks =date (' W ', $ftime);
if ($fweeks ==0) $fweeks = 7;
$nweeks =date (' W ');
if ($nweeks ==0) $nweeks = 7;
$ftemp =strtotime (Date (' y-m-d 00:00:00 ', $ftime))-$fweeks *60*60*24;
$ntemp =strtotime (Date (' y-m-d 00:00:00 ', Time ())) + (7-$nweeks) *60*60*24; echo Date (' W ', $ftemp). " <br/>....<br/> ". Date (' W ', $ntemp)."
<br/> ";
Return ($ntemp-$ftemp)/60/60/24/7;
function Daysofnow ($stime) {$ftime =strtotime ($stime);
Return Ceil ((Time ()-$ftime)/(60*60*24));
function Monthsofnow ($stime) {$ftime =strtotime ($stime);
$fmonth =date (' m ', $ftime);
$fyear =date (' Y ', $ftime);
$nmonth =date (' m ');
$nyear =date (' Y ');
$result = ($nyear-$fyear) *12+ $nmonth-$fmonth +1;
return $result; function Yearsofnow ($stime) {$ftimE=strtotime ($stime);
$fyear =date (' Y ', $ftime);
$nyear =date (' Y ');
Return $nyear-$fyear +1;
}//The following function is only for space use, not the core content, only for beautiful function lnbsp ($data, $num) {$result =trim ($data);
for ($i = $num; $i >=strlen ($data); $i-) {$result = ' '. $result;
return $result;
}?>
The third: tomorrow, next month and next year's date, you can use the following code:
$tomorrow = Date (' y-m-d ', Mktime (0,0,0,date ("M"), Date ("D") +1,date ("Y"));
$nextmonth = Date (' Y-m ', Mktime (0,0,0,date ("M") +1,date ("D") +1,date ("Y"));
$nextyear = date (' Y ', mktime (0,0,0,date ("M"), Date ("D"), date ("Y") +1));
echo $tomorrow. ' <br/> ';
echo $nextmonth. ' <br/> ';
echo $nextyear. ' <br/> ';
situation Fourth: working hours (excluding holiday)
?
$startDate = "2001-12-12";
$endDate = "2002-11-1";
$HOLIDAYARR =array ("05-01", "05-02", "10-01", "10-01", "10-02", "10-03", "10-04", "10-05", "01-26", "01-27", "01-28," "" 01-29 ");
An array of holiday dates, such as National Day, 51, Spring Festival
$endWeek =2;
Do you have a weekend break? The Double is 2, only Sunday rest is 1, no rest for 0
$beginUX =strtotime ($startDate);
$endUX =strtotime ($endDate);
for ($n = $beginUX; $n <= $endUX; $n = $n +86400) {
$week =date ("W", $n);
$MonDay =date ("m-d", $n);
if ($endWeek) {//Place weekend break
if ($endWeek ==2) {
if ($week ==0| | $week ==6) Continue
}
if ($endWeek ==1) {
if ($week ==0) Continue
}
}
if (In_array ($MonDay, $HOLIDAYARR)) continue;
$totalHour +=10;//work 10 hours a day
}
echo "Start date: $startDate <BR>";
echo "End Date: $endDate <BR>";
echo "spent a total of". $totalHour. " Hour ";
? >
Fifth: give a second count of hours
<?php
function Transform ($sec) {
$output = ';
$hours = Floor ($sec/3600);
$remainSeconds = $sec% 3600;
$minutes = Floor ($remainSeconds/60);
$seconds = $sec-$hours * 3600-$minutes *;
if ($sec >= 3600) {
$output. = $hours. ' H/';
$output. = $minutes. ' m/';
}
if ($sec >= && $sec < 3600) {
$output. = $minutes. ' m/';
}
return $output. = $seconds. ' s ';
}
Echo transform (3231803);
? >
The above is for you to provide all the examples of PHP date and time calculation, I hope to help you learn.