I have to create timestamp for 8 PM everyday to compare it with some timestamp in database. How can I create it?
我必须每天晚上8点创建时间戳,将其与数据库中的某个时间戳进行比较。我该如何创建它?
$ts = '2015-05-12 12:00:20'
is what I have stored in database.
$ ts ='2015-05-12 12:00:20'是我存储在数据库中的内容。
I need this $ts_8pm = '2015-05-12 20:00:00'
so that I can take some action when this meets.
我需要这个$ ts_8pm ='2015-05-12 20:00:00'以便我可以在这次会面时采取一些行动。
2 个解决方案
#1
It will be easier to create it dynamically each day:
每天动态创建它会更容易:
// Date-time will have value of 'now'
$dateTime = new DateTime();
// Override current time
$dateTime->setTime(20, 0);
$timestamp = $dateTime->getTimestamp();
Parsing a string makes very little sense, as you would have to generate that string first.
解析字符串没有多大意义,因为您必须首先生成该字符串。
#2
strtotime is specifically there for that purpose
strtotime专门用于此目的
strtotime — Parse about any English textual datetime description into a Unix timestamp
strtotime - 将任何英文文本日期时间描述解析为Unix时间戳
$ts_8pm=strtotime('2015-05-12 20:00:00');
You can also use this, better, approach
您也可以使用这种更好的方法
$format = 'Y-m-d H:i:s';
$date = DateTime::createFromFormat($format, '2015-05-12 20:00:00');
$ts_8pm=$date->format('U');
#1
It will be easier to create it dynamically each day:
每天动态创建它会更容易:
// Date-time will have value of 'now'
$dateTime = new DateTime();
// Override current time
$dateTime->setTime(20, 0);
$timestamp = $dateTime->getTimestamp();
Parsing a string makes very little sense, as you would have to generate that string first.
解析字符串没有多大意义,因为您必须首先生成该字符串。
#2
strtotime is specifically there for that purpose
strtotime专门用于此目的
strtotime — Parse about any English textual datetime description into a Unix timestamp
strtotime - 将任何英文文本日期时间描述解析为Unix时间戳
$ts_8pm=strtotime('2015-05-12 20:00:00');
You can also use this, better, approach
您也可以使用这种更好的方法
$format = 'Y-m-d H:i:s';
$date = DateTime::createFromFormat($format, '2015-05-12 20:00:00');
$ts_8pm=$date->format('U');