PHP strtotime function Usage detailed _php skill

Source: Internet
Author: User

This article illustrates the use of Strtotime functions in PHP. Share to everyone for your reference. Specifically as follows:

Strtotime (String $ time [, int $ now]) int strtotime (string $time [, int $now] The function expects a date format containing the U.S. English and tries to parse it into a Unix timestamp ( How many seconds since January 1, 1970 00:00:00 Monday of this format), relative to the time stamp provided now, or the current time if not now provided

This function will use the TZ environment variable (if any) to compute the timestamp, since the PHP 5.1.0 has an easier way to determine the time zone for all/datetime functions used, a process that is interpreted on a page of the Date_default_timezone_get () function.

parsed strings, in PHP 5.0.0, are not allowed in microseconds of time, since PHP 5.0.0 they are allowed, but ignored.

Now what is the time stamp used as the relative date of the calculation cardinality.

Return value: Returns a time stamp successfully, otherwise returns false, before going to PHP 5.1.0, this function will return failure-1.

Now let's take a look at the Strtotime character converted to a function instance of time with the following code:

Copy Code code as follows:
<?php
function
function Nextweeksday ($date _begin, $nbrweek)
{
$nextweek =array ();
for ($i = 1; $i <= $nbrweek; $i + +) {//week in one year of coursewww.phpfensi.com
$nextweek [$i]=date (' d d M Y ', strtotime (' + '. $i. ' Week ', $date _begin));
}
return $nextweek;
}
End Function
Example of a Select date
Var
$date _begin = strtotime (' 06-05-2010 '); D Day Month year-like function format.
$nbrweek = 52;
Call function
$result =nextweeksday ($date _begin, $nbrweek);
Preview
for ($i = 1; $i <= $nbrweek; $i + +) {
Echo ' <br>-'. $result [$i];
}
?>

<?php
$str = ' not good ';

Previous to PHP 5.1.0 you would compare with-1, instead of false
if (($timestamp = Strtotime ($str)) = = False) {
echo "The string ($STR) is bogus";
} else {
echo "$str = =". Date (' l DS o F Y h:i:s A ', $timestamp);
}
?>
<?php
Echo Strtotime ("Now"), "";
Echo Strtotime ("September 2000"), "";
Echo strtotime ("+1 Day"), "";
Echo strtotime ("+1 Week"), "";
Echo strtotime ("+1 Week 2 days 4 hours 2 Seconds"), "";
Echo Strtotime ("next Thursday"), "";
Echo Strtotime ("Last Monday"), "";
?>

This is a quick function calculation during the year, "Working days", "workdays" means those without weekends, no vacations in the $ array specified in the holiday, the instance code is as follows:
Copy Code code as follows:
function Get_working_days ($to _date) {
$holidays = Array (
1 => Array (10),//2011 ...
2 => Array (11),
3 => Array (21),//... 2011
4 => Array (29,30),//2010 ...
5 => Array (3,4,5),
6 => Array (),
7 => Array (19),
8 => Array (11,12,13),
9 => Array (20,23),
=> Array (11),
One => Array (3,23),
=> Array (23)//... 2010
);

For ($to _date, $w = 0, $i = 0, $x = time (); $x < $to _date $i + +, $x = Strtotime ("+ $i Day")) {
if (Date ("n", $x) < 6 &! In_array (Date ("J", $x), $holidays [Date ("n", $x)]) $w + +;
}
return $w;
}

Usage:
Echo get_working_days (Strtotime ("2011-01-08"));

I hope this article will help you with your PHP program design.

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.