Php date Operation TIPS and php Operation TIPS _ PHP Tutorial

Source: Internet
Author: User
Php date Operation TIPS and php Operation TIPS. Php date Operation TIPS, php Operation TIPS summary This article summarizes the php date operation skills. For your reference, I will share with you the following details: 1. php will summarize the date php date Operation TIPS obtained in the form, and php Operation TIPS

This example summarizes the php date operation skills. We will share this with you for your reference. The details are as follows:

1. php converts the date format obtained in the form to a uniform format.

2015-9-9 is converted to 2015-09-09 in a unified format in the database for later query

$ Year = "2015"; $ month = "9"; $ day = "09"; var_dump (checkdate ($ month, $ day, $ year )); // if (checkdate ($ month, $ day, $ year) === false) {exit ('error ');}; $ unixtime = mktime (2, 2, $ month, $ day, $ year ); // The purpose is to convert the data to a format with leading 0 in the month and day. The data is stored in a unified format. after the database is convenient, you can query var_dump (date ("Y-m-d ", $ unixtime); // Convert it to a timestamp in php, and then reverse it back.

2. obtain the start timestamp and end timestamp of the previous day.

The original idea is:

Use date to obtain the year, month, and day of the current day. Obtained separately. The expected year is 2015. the value is 9, and the value is 28.
Then subtract 1. But the problem arises.

If today is the 1st day. After subtracting 1, it becomes 0. Last month may be 28 or 30 days.

In this way, get the timestamp of the previous day. Let php automatically calculate.

Strtotime ("-1 day"); // Get the timestamp of the previous day. now, when is the timestamp of the previous day, the advantage of this method is that the above problem is solved, and php will automatically calculate the number of days in the previous month
<? Phpheader ("Content-type: text/html; charset = utf-8"); date_default_timezone_set ("Asia/Shanghai "); // Set the time zone $ last_day = strtotime ("-1 day"); // Obtain the timestamp of the previous day, the timestamp of the last day is obtained when it is now // the year, month, and day are obtained through the timestamp, so that mktime can use $ year = date ("Y", $ last_day ); $ month = date ("m", $ last_day); $ day = date ("d", $ last_day); $ last_day_begin = mktime (0, 0, 0, $ month, $ day, $ year); // timestamp starting from yesterday's day $ last_day_end = mktime (, 59, 59, $ month, $ day, $ year ); echo 'start time yesterday: '; var_dump ($ last_day_begin); echo date ('Y-m-d H: I: S', $ last_day_begin); echo'
'; Echo' Yesterday's end timestamp: '; var_dump ($ last_day_end); echo date ('Y-m-d H: I: S', $ last_day_end); echo'
'; Echo ($ last_day_end-$ last_day_begin)/(60*60); // exactly 24 hours

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.