Very comprehensive PHP date-time operations Rollup, PHP date Rollup _php tutorial

Source: Internet
Author: User
Tags echo date time and date

Very comprehensive PHP date-time Operations Summary, PHP date Summary


Before the example is explained, let's introduce several core functions:
Mktime function
The Mktime () function returns a Unix timestamp for a date.
The parameter always represents the GMT date, so IS_DST has no effect on the result.
Parameters can be left-to-right and empty, and empty parameters will be set to the corresponding current GMT value.
Syntax:mktime (HOUR,MINUTE,SECOND,MONTH,DAY,YEAR,IS_DST)
Parameter description
Hour is optional. Specified hours.
Minute is optional. Specify minutes.
Second is optional. Specify seconds.
Month is optional. Specifies the number of months to be represented.
Day is optional. Prescribed days.
Year is optional. Prescribed year. On some systems, the legal value is between 1901-2038. However, there is no such limit in PHP 5.
IS_DST is optional. If the time is in daylight saving time (DST), set to 1, otherwise set to 0 and if unknown, set to-1.
Since 5.1.0, the IS_DST parameter has been discarded. Therefore, you should use the new Time zone processing feature.
Example: the Mktime () function is useful for date arithmetic and validation. It can automatically correct out-of-bounds input:

Output:

Strtotime function
The strtotime () function resolves the datetime description of any English text to a Unix timestamp.
Syntax:strtotime (time,now)
Parameter description
TIME specifies a string of times to parse.
Now is used to calculate the timestamp of 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");
After January: Strtotime ("+1 months");
After one day: Strtotime ("+1 days");
30 seconds after Strtotime ("+30 seconds");
20 minutes later 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)

Example

The first case is that there is no database, just the date value to be compared, it must be fully used in PHP time and date function calculation, as follows:

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

The growth of the second kind of child

<? Date_default_timezone_set (' Asia/shanghai '); The above sentence to set the time zone, in fact, do not set the line, but Zde debug when there will be a hint, say what unsafe function ...  Add it up. echo Date (' y-m-d h:i:s '). ' Today is '. Date (' Y '). ' The year's '. Date (' W '). '  Weeks '; $stime = ' 2005-11-03 10:08 '; echo "

Since birth ($stime) ... :

"; echo "Today is the first". LNBSP (Daysofnow ($stime), 3). "Days
"; echo "Today is the first". LNBSP (Weeksofnow ($stime), 3). "Week
"; echo "Today is the first". LNBSP (Monthsofnow ($stime), 3). "Months
"; echo "Today is the first". LNBSP (Yearsofnow ($stime), 3). "Years
"; /* $output =sprintf ("Today is the first%03dDays
Today is section < font color=red>%03dWeek
Today is section < font color=red>%03dMonths
Today is section < font color=red>%03dYears
....
". Date (' W ', $ntemp)."
"; 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 function below is only used with spaces, not the core content, only for the aesthetic function lnbsp ($data, $num) {$result =trim ($data); for ($i = $num; $i >=strlen ($data); $i-) {$result = '. $result; } return $result; }?>

The third situation: 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. '
'; echo $nextmonth. '
'; echo $nextyear. '

Fourth situation: working hours (shaving 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, 51, Spring Festival $endWeek =2;  Do you have a double break for the weekend? 2, only Sunday rest for 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 per day} echo start date: $startDate
"; echo "End Date: $endDate


Fifth case: 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 to provide you with all of the PHP date and time calculation of all instances, I hope that everyone's learning has helped.

http://www.bkjia.com/PHPjc/1067825.html www.bkjia.com true http://www.bkjia.com/PHPjc/1067825.html techarticle A very comprehensive PHP date-time operation summary, the PHP date summary example before the introduction of several core functions: Mktime function Mktime () function returns a date of the Unix timestamp. ...

  • Related Article

    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.