PHP Date Manipulation Tips Summary

Source: Internet
Author: User
Tags echo date set time
This article mainly introduces the PHP date manipulation skills, involving the PHP date and time display format conversion skills, with a certain reference value, the need for friends can refer to the next

This article summarizes the PHP date manipulation techniques. Share to everyone for your reference, as follows:

1. PHP converts the date format obtained in the form into a uniform format

2017-7-9 all unified into the 2015-09-09 so in the database are unified format, convenient for later query

$year = "", $month = "7"; $day = "Var_dump"; Checkdate ($month, $day, $year));//month and day with a leading 0 are in accordance with the format if (Checkdate ($month, $day, $year) {===false) {  exit (' Error ');}; $unixtime = Mktime (2,2,2, $month, $day, $year);//The purpose is to give PHP a format that is converted into months and days with a leading 0 formatted uniform format stored in the database for easy querying later var_dump ("Y-m-d", $ Unixtime));////to PHP to convert to timestamp, then reverse back

2. Get the start timestamp and end timestamp for the previous day

The original idea is:

Use date to get the date of the day. obtained separately. Get year is 2015 months is 9th is 28
then subtract 1. But the problem came out.

If it is number 1th today. Minus 1 turns into 0. Last month could be 28 days, or maybe 30 days.

This way, first get the time stamp of the previous day. Let PHP automatically calculate.

Strtotime ("1 day");//Get the time stamp of the day, and now it's time to get the timestamp of the time of the day, in this way the benefit is to solve the above problem, PHP will automatically calculate how many days last month
<?phpheader ("Content-type:text/html;charset=utf-8");d ate_default_timezone_set ("Asia/shanghai");//Set time zone $last_ Day = Strtotime ("-1 Days");//Get the time stamp of the previous one, and now it's time to get the timestamp of the time of the last day//time stamp to get the date, so that mktime 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);//start of day yesterday's time stamp $ Last_day_end = Mktime (23,59,59, $month, $day, $year); Echo ' Yesterday start timestamp: '; Var_dump ($last _day_begin); Echo Date (' y-m-d h:i:s ' , $last _day_begin); Echo ' <br/> '; Echo ' Yesterday end timestamp: '; Var_dump ($last _day_end); Echo Date (' y-m-d h:i:s ', $last _day_end echo ' <br/> '; 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.