Usage of strtotime function in php

Source: Internet
Author: User
This article mainly introduces the usage of strtotime function in php, and analyzes the parameter meaning and corresponding usage of strtotime function in instance form in detail, which is suitable for time format conversion, for more information about strtotime function usage in php, see the following example. Share it with you for your reference. The details are as follows:

Strtotime (string $ time [, meaning $ now]) int strtotime (string $ time [, int $ now] This function is expected to get a date format that contains American English, and will try to parse it into a Unix timestamp (the number of seconds since 00:00:00 Monday, January 1, January 1, 1970), relative to the current timestamp, or if the current time is not provided

This function uses the TZ environment variable (if any) to calculate the timestamp. PHP 5.1.0 has an easier way to determine the time zone of All/dateTime functions used, this process is explained on the page of the date_default_timezone_get () function.

The parsed strings in PHP 5.0.0 are not allowed in microseconds. they are allowed from PHP 5.0.0, but ignored.

Which of the following timestamps are used as the calculation base relative to the date.

Returned value: a timestamp is returned successfully. otherwise, FALSE is returned. the previous value is PHP 5.1.0. This function will return failure-1.

Now let's take a look at the function instance for converting strtotime to time. the code is as follows:

The code is as follows:

<? Php
// Function
Function nextWeeksDay ($ date_begin, $ nbrweek)
{
$ Nextweek = array ();
For ($ I = 1; $ I <= $ nbrweek; $ I ++) {// 52 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'
-'. $ Result [$ I];
}
?>

<? Php
$ Str = 'not Good ';

// Previous to PHP 5.1.0 you wowould 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 ("10 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 fast function compute period of one year, "working days", "working days" refers to those without weekends, no holidays in the $ array specified holidays, the instance code is as follows:

The code is as follows:

Function get_working_days ($ to_date ){
$ Holidays = array (
1 => array (10), // 2011...
2 => array (11 ),
3 => array (21), //... 2011
4 => array (2010...
5 => array (3, 4, 5 ),
6 => array (),
7 => array (19 ),
8 => array (11,12, 13 ),
9 => array (20, 23 ),
10 => array (11 ),
11 => array (3, 23 ),
12 => 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 PHP programming.

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.