Php date to timestamp, specifying date to timestamp

Source: Internet
Author: User
Tags timestamp to date
The UNIX timestamp and the formatting date are two time representation forms that we often deal with. The Unix timestamp is easy to store and process, but not intuitive, and the formatting date is intuitive, however, it is not as easy to process as Unix timestamps, so sometimes mutual conversion is required. the following section describes the time difference between programmers who have transferred PHP date to PHP + MySQL, the UNIX timestamp and the formatting date are two time representation forms that we often deal with. The Unix timestamp is easy to store and process, but not intuitive, and the formatting date is intuitive, but it is not as easy to process as Unix timestamps, so sometimes mutual conversion is required. below are several conversion methods for mutual conversion.

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: select FROM_UNIXTIME (1156219870 );
2. convert a date to a UNIX timestamp using the function: UNIX_TIMESTAMP ()
General format: Select UNIX_TIMESTAMP ('2017-11-04 12:23:00 ′);
Example: mysql queries the number of records on the current day:
$ 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 ');

Php date to timestamp, specifying date to timestamp

Php date to timestamp, specified date to timestamp, PHP scheduled task.
In the past two days, we need to implement the following functions:
When a certain condition is reached, the server sends a text message to the user. the number of messages is multiple.
Basic idea: scheduled linux Scanning. if a user meets the requirements, the user will send a text message.
However, to prevent disturbing users, you must send text messages between-during the day. how can you get the daily time range?
The following code:
The code is as follows:
$ 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 ....
}
?>

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.