php date to timestamp specify the date to be converted to timestamp

  • 2020-05-19 04:22:40
  • OfStack

Written PHP + MySQL programmers know time difference, UNIX timestamp and format the date is two time representation, we often deal with Unix timestamp stored, easy processing, but not intuitive, formatting date intuitive, but processing as well as Unix timestamp freely, so sometimes need to convert each other, mutual conversion transformation ways is given below.

1. Do it in MySQL

This method is transformed in MySQL query statements. The advantage is that it does not take up the parsing time of PHP parser, which is fast. The disadvantage is that it can only be used in database queries, which has limitations.
1. UNIX time stamp converted to date function: FROM_UNIXTIME()
1 general form: select FROM_UNIXTIME(1156219870);
2. Date conversion to UNIX time stamp function: UNIX_TIMESTAMP()
1 general form: Select UNIX_TIMESTAMP('2006-11-04 12:23:00 ');
Example: the number of records mysql queried on 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, so let's talk about converting in PHP.

2. Do it in PHP

This method completes the conversion in PHP program. The advantage is that the data obtained from the query in the database can be converted whether or not, and the conversion range is not limited. The disadvantage is that it takes up the parsing time of PHP parser, and the speed is relatively slow.
1. UNIX time stamp converted to date function: date()
1 general form: date(' Y-m-d H:i:s', 1156219870);
2. Date conversion to UNIX time stamp function: strtotime()
1 general form: strtotime('2010-03-24 08:15:42');

php date to time stamp, the specified date to be converted to time stamp

php date to time stamp, specified date to time stamp, PHP timing task.
In these two days, the following functions will be implemented:
When a certain 1 condition is reached, let the server send a message to the user, the number is multiple.
Basic idea: linux regularly scans, and sends SMS messages to users who meet the requirements.
However, in order to avoid disturbing users, it is required to send SMS messages only between 8:00 and 20:00 during the day. How can I get to this time of day?
The following 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.... 
} 
?> 


Related articles: