简体   繁体   English

转换为24小时格式,然后加上时间戳

[英]convert to 24 hour format then timestamp

My datetime formatting is not working. 我的日期时间格式不起作用。 I need to convert a 12 hour format time to 24 hour format after that the time's timestamp is required. 在需要该时间的时间戳之后,我需要将12 hour format时间转换为24 hour format so my code is 所以我的代码是

$to =  DateTime::createFromFormat('h:i A','1:00 AM');
echo $to->format('d M y H:i').'<br/>'; 
echo $to->getTimestamp();  

My 2nd line code's desired result is 24 hour format of 28 Sep 15 1:00 AM and then timestamp of this datetime. 我的第二行代码的理想结果是28 Sep 15 1:00 AM 24小时格式,然后是该日期时间的时间戳。 Whats wrong in my code? 我的代码有什么问题?

My Problem is the timestamp of 28 Sep 15 1:00 AM is smaller than the timestamp of 28 Sep 15 8:00 PM . 我的问题是28 Sep 15 1:00 AM的时间戳小于28 Sep 15 8:00 PM的时间戳。 But WHY? 但为什么?

https://en.wikipedia.org/wiki/Unix_time https://zh.wikipedia.org/wiki/Unix_time

... defined as the number of seconds that have elapsed since 00:00:00 Thursday, 1 January 1970 ...定义为自1970年1月1日,星期四00:00:00起经过的秒数

-- -

My Problem is the timestamp of 28 Sep 15 1:00 AM is smaller than the timestamp of 28 Sep 15 8:00 PM. 我的问题是,9月28日1:00 AM的时间戳小于28 Sep 15 8:00 PM的时间戳。 But WHY? 但为什么?

Being timestamp "the number of seconds that have elapsed since 00:00:00 Thursday, 1 January 1970" obviously 28 Sep 15 1:00 AM will always be smaller than 28 Sep 15 8:00 PM the same way that 28 Sep 15 00:00 AM will always be smaller than 28 Sep 15 00:00 PM 作为时间戳“自1970年1月1日星期四00:00:00起经过的秒数”很明显,9月28日15 AM总是比9月15日8:00 PM小,就像9月28日15 00 :00 AM将始终小于15年9月28日下午 00:00

Add this line after the first one: 在第一行之后添加以下行:

$to->modify('+12 hours');

EDIT: I'm not really getting what you need exactly, so here you have some samples: 编辑:我并没有真正得到您所需要的,因此这里有一些示例:

$am = DateTime::createFromFormat('h:i A','1:00 AM');
echo $am->format('d M y H:i');   // prints '28 Sep 15 01:00'
echo $am->format('d M y h:i A'); // prints '28 Sep 15 01:00 AM'

$pm = DateTime::createFromFormat('h:i A','8:00 PM');
echo $pm->format('d M y H:i');   // prints '28 Sep 15 20:00'
echo $pm->format('d M y h:i A'); // prints '28 Sep 15 08:00 PM'

Code sample here: https://3v4l.org/PvAGm 此处的代码示例: https : //3v4l.org/PvAGm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM