PHP Date Operation techniques Summary _php Tips

Source: Internet
Author: User
Tags echo date php programming php regular expression set time

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

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

2015-9-9 is unified into 2015-09-09 so in the database are unified format, easy to query later

$year = "2015";
$month = "9";
$day = "a";
Var_dump (Checkdate ($month, $day, $year))//month and day with leading 0 are in line with the format
if (Checkdate ($month, $day, $year) ===false) {
  Exit (' error ');
$unixtime = Mktime (2,2,2, $month, $day, $year);//The purpose is to convert PHP into months and days with a format of leading 0 in a uniform format stored in the database to facilitate query
Var_dump (date ("y-m-d" , $unixtime));
Convert PHP to timestamp, then reverse back

2. Get start time stamp and end timestamp for last day

Originally thought is:

Use date to get the date of the day. Individually acquired. The year is 2015 months, 9th is 28.
And then minus 1 is OK. But the problem comes out.

If today is number 1th. Minus 1 becomes 0. Last month could have been 28 days, or 30 days.

In this way, get the time stamp of the previous day first. Let PHP automatically compute.

Strtotime ("-1 day");
Get the time stamp of the day, now is the time to get the time stamp of this point in the day, in this way the advantage is to solve the above problem, PHP will automatically calculate how many days last month

<?php
Header ("Content-type:text/html;charset=utf-8");
Date_default_timezone_set ("Asia/shanghai");/set time zone
$last _day = Strtotime ("-1 day"); Now is the time to get the last day of this point timestamp
//time stamp to get the month and year, 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); the timestamp of yesterday's day began
$last _day_end = Mktime (23,59,59, $month, $day, $ year);
Echo ' Yesterday started time stamp: ';
Var_dump ($last _day_begin);
echo Date (' y-m-d h:i:s ', $last _day_begin);
echo ' <br/> ';
Echo ' Yesterday's End time stamp: ';
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

For more information about PHP interested readers can view the site topics: "PHP date and Time usage summary", "PHP object-oriented Programming Introductory Course", "PHP Mathematical Calculation Skills Summary", "PHP operation Office Document Skills Summary (including word,excel,access , PPT), the "PHP array" operation Skills Encyclopedia, "PHP Data structure and algorithm tutorial", "PHP Programming Algorithm Summary", "PHP Regular Expression Usage Summary", and "PHP Common database Operation Skills Summary"

I hope this article will help you with the 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.