【问题标题】:How to properly convert date如何正确转换日期
【发布时间】:2014-01-03 05:45:32
【问题描述】:

我正在尝试为最新的日期条目安排我的日期,我有一个日期格式并将其设置为 ISO-8601 年份编号。

$currentdate =mktime(date('H'),date('i'),date('s'),date('m'),date('d'),date('o'));

我需要存储数据库中的值,因为当前日期的最后一个月是 2013 年 12 月我希望我的时间戳得到相同的值。 不幸的是,我得到了一个不同的日期,我的 $currentdate 在时间戳(1420022147)中的值。当我尝试使用

查看我的日期时
$val ="1420022147";
date_default_timezone_set('Asia/Manila');
echo date('M-d-Y H:i:s',$val);
I get this Dec-31-2014 18:35:47

我是不是用错了函数? 如何将我的一年转至 2013 年?

【问题讨论】:

  • 不确定你在用 $currentdate 变量做什么
  • 此外,问题出在某处,1420022147 确实是 2014 年 12 月 31 日。所以你的问题出在你设置 $val 的任何地方。告诉我们你在哪里/如何设置$var
  • 感谢您的回复,对于不清楚的变量声明,我深表歉意。我的 $currentdate 用于使用 mktime 将值存储在我的数据库中。至于我的 $val,我仅以此作为示例,展示了我从数据库中获得的值。希望我回答了你的问题。

标签: php


【解决方案1】:

获取当前时间戳使用 strtotime 函数如下

$current_time = strtotime(date('d-M-Y g:i:s A')); // eg : 1388729310

如果你想转换成日期,你可以使用任何你想要的日期格式

echo date('d-M-Y g:i:s A', $current_time); // output : 03-Jan-2014 6:08:30 AM

【讨论】:

    【解决方案2】:

    我不完全理解你的问题,但我认为你需要在设置date_default_timezone_set 之后设置$currentdate$currentdate也可以通过time()函数轻松生成

    date_default_timezone_set('Asia/Manila');
    $currentdate = time();
    $val = $currentdate;
    echo date('M-d-Y H:i:s',$val);
    

    【讨论】:

      猜你喜欢
      • 2015-09-10
      • 2017-10-02
      • 1970-01-01
      • 2016-05-20
      • 1970-01-01
      • 2017-08-31
      相关资源
      最近更新 更多