Home>

Before the example is explained,Let's introduce some core functions first:

mktime function

The mktime () function returns the unix timestamp of a date.

The parameter always represents the gmt date, so is_dst has no effect on the result.

Parameters can be left blank from right to left,An empty parameter is set to the corresponding current gmt value.

Syntax:mktime (hour, minute, second, month, day, year, is_dst)Parameter Descriptionhour Optional. Prescribed hours.

minute Optional. Specify the minutes.

second is optional. Specify seconds.

month Optional. Specifies the month as a number.

day is optional. Prescribed day.

year Optional. Define the year. On some systems,Legal values ​​are between 1901-2038. However, this limitation no longer exists in PHP 5.

is_dst Optional. If the time is during the daylight saving time (dst), set it to 1, otherwise set it to 0, if not known, set it to -1.

As of 5.1.0, the is_dst parameter is deprecated.So you should use the new time zone processing feature.

Example:The mktime () function is very useful for date arithmetic and validation.It can automatically correct out-of-bounds inputs:

<?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 functionThe strtotime () function parses the datetime description of any English text into a unix timestamp.

Syntax:strtotime (time, now)

Parameter Descriptiontime specifies the time string to be parsed.

now is used to calculate the timestamp of the return value.If this parameter is omitted,The current time is used.

After one week:strtotime ("+ 1 week");

A week ago:strtotime ("-1 week");

After one month:strtotime ("+ 1 months");

After one day:strtotime ("+ 1 days");

Strtotime ("+30 seconds") after 30 seconds;

After 20 minutes strtotime ("+20 minutes");

After 12 hours 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 used in the script.

date_default_timezone_set (timezone)

Examples

First caseNo database,Just comparing the obtained date values,Then you have to calculate it completely with PHP's time and date function.as follows:

For example, to calculate how many days from 2015-9-5 to 2015-9-18:

<?php
$startdate=strtotime ("2015-9-5");
$enddate=strtotime ("2015-9-18");//The above PHP time and date function has turned the date into a timestamp,It just became seconds.
As long as the two values ​​are subtracted,Then you can turn seconds into days,Relatively simple,as follows:
$days=round (($enddate- $startdate)/3600/24);
echo $days;//days is the number of days obtained;
?>

SecondChildren's growth

<?
date_default_timezone_set ("asia/shanghai");
//The above sentence is for setting the time zone,In fact, it is not necessary to set up,But there will be a prompt when zde debug,Say something unsafe ... add it.
echo date ("y-m-d h:i:s"). "Today is" .date ("y"). "The first" .date ("w"). "week" of the year;
$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 the first<font color=red><b>%03d</b></font>Day<br />Today is the<font color=red><b>%03d</b></font>week<br />today is the first<font color=red><b>%03d</b></font>month<br />Today is the first<font color=red><b>%03d</b></font>year<br />", 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 (abs ((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 used to add spaces,Not the core content,Just for beauty
function lnbsp ($data, $num)
{
 $result=trim ($data);
 for ($i=$num;$i>= strlen ($data);$i--) {
 $result="". $result;
 }
 return $result;
}
?>

The third case:Tomorrow, next month and next year,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 />";

The fourth case:Working hours (excluding holidays)

<?
$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 ");
 //holiday date array,For example National Day,May Day, Chinese New Year, etc.
$endweek=2;
 //Whether weekends are closed.
2 for weekends, 1 for Sundays only, 0 for no holidays
$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) {//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 "Total spent". $totalhour. "Hour";
?>

Fifth case:Give seconds as hours

<?php
function transform ($sec) {
 $output="";
 $hours=floor ($sec/3600);
 $remainseconds=$sec%3600;
 $minutes=floor ($remainseconds/60);
 $seconds=$sec-$hours * 3600-$minutes * 60;
 if ($sec>= 3600) {
 $output.=$hours. "h /";
 $output.=$minutes. "m /";
 }
 if ($sec>= 60&&$sec<3600) {
 $output.=$minutes. "m /";
 }
 return $output.=$seconds. "s";
}
echo transform (3231803);
?>
php
  • Previous Analysis of Usage of References in Android Programming
  • Next Detailed explanation of singletast activation mode of Activity and how to use intent to pass values