Programmers who write Php+mysql know there is a time difference, UNIX timestamp and format date are two time representations we often deal with, Unix timestamp storage, processing convenient, but not intuitive, format date intuitive, but not as easy to deal with the Unix time stamp so freely, So there are times when you need to convert to each other, and there are several ways to transform each other.
one, complete in MySQL
This way in the MySQL query statement conversion, the advantage is not to occupy PHP parser parsing time, fast, the disadvantage is only used in database query, there are limitations.
1. UNIX timestamp converted to date function: From_unixtime ()
General form: Select From_unixtime (1156219870);
2. Date conversion to UNIX timestamp with function: Unix_timestamp ()
General form: Select unix_timestamp (' 2006-11-04 12:23:00′);
For example: MySQL Query the number of records of the 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 is said in PHP conversion.
second, in PHP to complete
This way in the PHP program to complete the conversion, the advantage is that no matter is not the database query to obtain the data can be converted, the scope of the conversion is unrestricted, the disadvantage is to occupy the parsing time of the PHP parser, the speed is relatively slow.
1. UNIX timestamp converted to date function: Date ()
General form: Date (' y-m-d h:i:s ', 1156219870);
2. Date conversion to Unix timestamp with function: Strtotime ()
General form: Strtotime (' 2010-03-24 08:15:42 ');
PHP Date-time stamp, specifying date converted to timestamp
PHP date to time stamp, specified date converted to timestamp, PHP timed task.
These two days to achieve this function:
When a certain condition is reached, let the server send text messages to the user, the number is more than one.
Basic idea: Linux regular scan, if there are users to meet the conditions, then send text messages.
But in order to prevent disturbing to the user, the request can only 8:00-20:00 send text messages during the day, how to get to this time interval every day?
The following code:
Copy Code code 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 (0, 0, $m, $d, $y);
$time = time ();
if ($time >= $start _time && $time <= $end _time)
{
Do something ....
}
?>