简体   繁体   English

PHP strtotime返回false

[英]PHP strtotime returns false

I have a jquery function which passes the date '27/05/2016 11:25 PM' to a PHP file. 我有一个jQuery函数,它将日期“ 27/05/2016 11:25 PM”传递到PHP文件。 The PHP file will update this to the database. PHP文件会将其更新到数据库。

I'm using strtotime to convert this string to date format '05/27/2016 11:25 PM' but strtotime returns false. 我正在使用strtotime将此字符串转换为日期格式为'05 / 27/2016 11:25 PM',但是strtotime返回false。

My PHP date conversion: 我的PHP日期转换:

$EndDate = strtotime($Date);
$NewEndDateValue = date('m/d/Y h:i A', $EndDate);

var_dump($NewEndDateValue); //this returns false

strtotime() by default treats dates with / seperators as the wierd USA format for dates, where they start in the middle of a date and work outwards from there (go figure). 默认情况下, strtotime()将使用/分隔符的日期视为日期的美国格式,从日期的中间开始,然后从那里开始(如图)。 Its fine to speak it that way but totally illogical to expect a logic machine (computer) to work that way. 这样说很好,但是期望逻辑机(计算机)那样工作完全不合逻辑。

Anyway, all you need to do is convert the / to a - and date() will assume a logical date format and therefore work. 无论如何,您要做的就是将/转换为-并且date()将采用逻辑日期格式,因此可以正常工作。

<?php
$Date = '27/05/2016 11:25 PM';
$dat = str_replace('/', '-', $Date);
$EndDate = strtotime($dat);

$NewEndDateValue = date('m/d/Y h:i A', $EndDate);

var_dump($NewEndDateValue); // "05/27/2016 11:25 PM"

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

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