Php date to timestamp, specifying date to timestamp

Source: Internet
Author: User
Tags ini mysql query timestamp to date

I. Completed in MySQL

This method is converted into a MySQL Query statement. The advantage is that it does not take up the parsing time of the PHP parser and is fast. The disadvantage is that it can only be used in database queries and has limitations.
1. UNIX timestamp conversion to date function: FROM_UNIXTIME ()
General format:

The code is as follows: Copy code
Select FROM_UNIXTIME (1156219870 );

2. Convert a date to a UNIX timestamp using the function: UNIX_TIMESTAMP ()
General format:

The code is as follows: Copy code
Select UNIX_TIMESTAMP ('2017-11-04 12:23:00 & prime ;);

Example: mysql queries the number of records on the current day:

The code is as follows: Copy code

$ SQL = "select * from message Where DATE_FORMAT (FROM_UNIXTIME (chattime), '% Y-% m-% d') = DATE_FORMAT (NOW (), '% Y-% m-% d') order by id desc ";

Of course, you can also choose to convert in PHP. The following describes the conversion in PHP.

2. Complete in PHP

This method completes the conversion in the PHP program. The advantage is that no matter whether the data obtained by queries in the database can be converted, the conversion range is unrestricted, and the disadvantage is that it takes up the parsing time of the PHP parser, relatively slow.
1. Convert UNIX timestamp to date using the function: date ()
General format: date ('Y-m-d H: I: S', 1156219870 );
2. Convert a date to a UNIX timestamp using the function strtotime ()
General format:


Strtotime ('2017-03-24 08:15:42 ');

Example

The code is as follows: Copy code

<? Php
Class SaonekController extends Controller {

Public function indexAction (){
/* It is unnecessary to convert the timestamp into a date.
However, to convert a date to a timestamp, strtotime () is required ()
*/
$ Time = time (); // timestamp
$ Nowtime = date ('Y-m-d H: I: S', $ time); // Generate a date with a format
$ Oldtime = '2017-11-10 22:19:21 ';
$ Export me = strtotime ($ oldtime); // Convert a date to a timestamp
$ Nowtimes = date ('Y-m-d H: I: S', $ hour me); // The timestamp is converted back to the date.
Echo $ nowtimes;
 }
}

?>


It is required that you only send text messages between-in the daytime. How can you get the daily time range?

The following code:

The code is as follows: Copy code


<?
$ Y = date ("Y", time ());
$ M = date ("m", time ());
$ D = date ("d", time ());
$ Start_time = mktime (9, 0, 0, $ m, $ d, $ y );
$ End_time = mktime (19, 0, 0, $ m, $ d, $ y );
$ Time = time ();
If ($ time >=$ start_time & $ time <= $ end_time)
{
// Do something ....
}
?>

Some usage that you don't know

The code is as follows: Copy code

<? Php

# On 2/8/2010
Date ('m/d/Y', strtotime ('First Day'); #02/01/10
Date ('m/d/Y', strtotime ('last day'); #02/28/10
Date ('m/d/Y', strtotime ('last day next month'); #03/31/10
Date ('m/d/Y', strtotime ('last day last month'); #01/31/10
Date ('m/d/Y', strtotime ('2017-12 last day ')); #12/31/09-this doesn't work if you reverse the order of the year and month
Date ('m/d/Y', strtotime ('2017-03 last day'); #2009/09
Date ('m/d/Y', strtotime ('2017-03'); #2009/09
Date ('m/d/Y', strtotime ('Last day of march 100'); #2009/09
Date ('m/d/Y', strtotime ('Last day of March'); #03/31/10
?>


Note: The timestamp above php5.1 is 8 hours different from the actual time. The solution is as follows:

1. The simplest method is not to use php5.1 or a later version-obviously this is not an advisable method !!!

2. Modify php. ini. Open php. ini to search for date. timezone and remove the semicolon = followed by Asia/Shanghai. Restart the apache server. The disadvantage is that if the program
Put it on someone else's server and cannot modify php. ini.

3. Add the initialization statement of time to the program: "date_default_timezone_set (" Asia/Shanghai ");" which can be set by the programmer at will.
Recommendation.
Time zone identifier, available value in mainland China: PRC, Asia/Chongqing, Asia/Shanghai, Asia/Urumqi (China, Chongqing, Shanghai, Urumqi), Etc/GMT-8, asia/Harbin
Available in Hong Kong and Taiwan regions: Asia/Macao, Asia/Hong_Kong, Asia/Taipei (in the order of Macao, Hong Kong, and Taipei)
Singapore: Asia/Singapore

 

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.