Home >Backend Development >PHP Tutorial >Convert date to timestamp in php, convert specified date to timestamp

Convert date to timestamp in php, convert specified date to timestamp

不言
不言Original
2018-06-06 11:29:313991browse

This article mainly introduces about converting php date to timestamp, converting a specified date into a timestamp, which has a certain reference value. Now I share it with everyone. Friends in need can refer to it

UNIX timestamp Formatted dates and formatted dates are two time representations that we often deal with. Unix timestamps are easy to store and process, but not intuitive. Formatted dates are intuitive, but not as easy to process as Unix timestamps, so sometimes they need to be converted to each other. The following are several conversion methods for converting PHP date to timestamp and MySQL date conversion functions to each other

Programmers who have written PHP MySQL all know that there is a time difference. UNIX timestamps and formatted dates are what we often deal with. Two time representations, Unix timestamp is convenient to store and process, but not intuitive. Formatted date is intuitive, but it is not as easy to process as Unix timestamp, so sometimes it is necessary to convert each other. Here are several conversions for each other. Way.
1. Complete in MySQL  
This method is converted in the 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 for database queries. , there are limitations.
1. Use the function to convert UNIX timestamp to date: FROM_UNIXTIME()
General form: select FROM_UNIXTIME(1156219870);
2. Use the function to convert date to UNIX timestamp: UNIX_TIMESTAMP()
General Format: Select UNIX_TIMESTAMP('2006-11-04 12:23:00′);
Example: mysql query the number of records 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, as follows Let’s talk about conversion in PHP.
2. Complete in PHP  
This method completes the conversion in the PHP program. The advantage is that it can be converted regardless of whether the data obtained by querying in the database is, and the conversion range is not limited. The disadvantage is that It takes up the parsing time of the PHP parser and is relatively slow.
1. Use the function to convert UNIX timestamp to date: date()
General form: date('Y-m-d H:i:s', 1156219870);
2. Use the function to convert date to UNIX timestamp :strtotime()
General form: strtotime('2010-03-24 08:15:42');
php date to timestamp, the specified date is converted to a timestamp
PHP date to timestamp, specified date to timestamp, PHP scheduled tasks.
In the past two days, we need to implement this function:
When a certain condition is reached, let the server send multiple text messages to the user.
Basic idea: Linux scans regularly, and if there are users who meet the conditions, send text messages.
However, in order to prevent disturbing users, it is required that text messages can only be sent between 8:00-20:00 during the day. How to obtain this time range every day?
The following code:

Copy code The code is as follows:

<? php
$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 recommendations:

php Common date phase functions [Date addition and subtraction, difference between two dates, date conversion time cutoff]

PHP date and time, PHP date time


The above is the detailed content of Convert date to timestamp in php, convert specified date to timestamp. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn