【问题标题】:Errors with using a timestamp in PHP在 PHP 中使用时间戳的错误
【发布时间】:2015-05-26 17:56:02
【问题描述】:

我收到错误:遇到格式不正确的数值

这是我的代码:

<?php

$timestamp= '2013-01-20 18:20:20';

$datetime= date('F j, Y', $timestamp);

    echo $datetime;

?>

这将返回 1970 年 1 月 1 日,这是不对的。我究竟做错了什么? 顺便说一句:我所有的 $timestamp 变量都将采用这种格式。我在我的 MySQL 数据库表中使用日期时间。

谢谢

【问题讨论】:

  • 也为了澄清,有几个“时间戳”(每个数据库都使用自己的)。 php 使用“unix 时间戳”,它表示自“1970 年 1 月 1 日 00:00:00 UTC”以来的秒数。 OP 使用的时间戳不是有效的“unix 时间戳”,因此 date() 认为传入的“unix 时间戳”为 0,表示自“1970 年 1 月 1 日”以来的 0 秒。 en.wikipedia.org/wiki/Unix_time

标签: php


【解决方案1】:

date 函数采用一个 int 时间戳 您需要使用

调用它
date('F j, Y', time());

$timestamp= '2013-01-20 18:20:20';
date('F j, Y', strtotime($timestamp));

有关如何使用date 函数的信息,请参阅http://php.net/manual/en/function.date.php

【讨论】:

    【解决方案2】:

    '2013-01-20 18:20:20' 不是时间戳。您必须将其转换为时间戳。您可以使用 strtotime 函数来执行此操作。

    $timestamp= strtotime('2013-01-20 18:20:20');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-08
      • 1970-01-01
      • 1970-01-01
      • 2018-06-09
      • 1970-01-01
      • 2013-03-30
      • 2020-07-03
      相关资源
      最近更新 更多