【问题标题】:PHP strtotime() returns false for JavaScript Date stringPHP strtotime() 为 JavaScript 日期字符串返回 false
【发布时间】:2012-09-03 06:58:32
【问题描述】:

在浏览器中,在 JS 中

var today = new Date()
todaySendToServer = today.toString();

我正在通过 AJAX 调用或作为 URL 的一部分向服务器发送 todaySendToServer。

在服务器上,在 PHP 中:

$todayJsDateString1 = preg_replace('#^(\d+)/(\d+)/(\d+)\s([\d:]+)\s([a-zA-z]{0,2})$#','$3-    $2-$1 $4 $5', $todayJsDateString); 
$todayTimestamp = strtotime(todayJsDateString1);

对于某些浏览器(如 IE9)返回的日期字符串,strtotime() PHP 调用返回 false

有没有其他方法可以实现这一点?

【问题讨论】:

  • 你能按照标准格式化你的字符串吗?例如 ISO8601
  • 如何将 JavaScript 日期字符串格式化为 ISO8601?有 API 吗?

标签: php javascript date strtotime


【解决方案1】:

改为传递时间戳,因此您甚至不需要在 php 端使用strtotime

var today = new Date();
var ts = today.getTime() / 1000;

【讨论】:

    【解决方案2】:

    我猜这只是一个简单的错字,你错过了一个美元符号:

    $todayJsDateString1 = preg_replace('#^(\d+)/(\d+)/(\d+)\s([\d:]+)\s([a-zA-z]{0,2})$#','$3-    $2-$1 $4 $5', $todayJsDateString); 
    $todayTimestamp = strtotime($todayJsDateString1);
    

    【讨论】:

    • IE9 浏览器发送的 JS 日期字符串无效。谢谢
    【解决方案3】:

    为什么不把它作为时间戳一路发送?

    var timestamp = new Date().getTime() / 1000;
    
    $todayTimestamp = $todayJsDateString;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-17
      • 2011-12-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多