doucai9270 2015-12-27 10:35
浏览 63
已采纳

使用固定结果将字符串日期转换为时间戳和时间戳到目前为止[复制]

This question already has an answer here:

I have an string date like this: 2015/12/20 13:58:59

I try to convert timestamp:

$idate = $user->multiexplode(array("/"," ",":"),strip_tags("2015/12/20 13:58:59"));

//mktime( $hour , $minute , $second , $month , $day , $year , $is_dst );
$timestamp = mktime($idate[3],$idate[4],$idate[5],$idate[1],$idate[2],$idate[0]);

And now I try to convert real date:

echo 'new date: '.jdate('Y/n/j H:i:s',$timestamp);

Ok...it works but there is a problem!

according of time server,I get variable time.

for examle for location +1 GMT: 2015/12/20 14:58:59

for -1 GMT: 2015/12/20 11:58:59

I want for all server print 2015/12/20 13:58:59 again

</div>
  • 写回答

2条回答 默认 最新

  • duanquan4451 2015-12-27 11:03
    关注

    You can use DateTime class and it's methods to convert the date and time to unix timestamp. And use strftime() to convert the unix timestamp back to your desired format, like this:

    $datetime = "2015/12/20 13:58:59";
    
    // covert timestamp
    $unixdatetime = DateTime::createFromFormat('Y/m/d H:i:s', $datetime)->getTimestamp();
    echo $unixdatetime . "<br />";  // 1450616339
    
    // now format the unix timestamp
    $formatted_datetime = strftime("%Y/%m/%d, %H:%M:%S",$unixdatetime);
    echo $formatted_datetime;  // 2015/12/20, 13:58:59
    

    Output:

    1450616339
    2015/12/20, 13:58:59
    

    Here are the references:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 esp8266控制共阳极wrgb灯板无法关闭所有led灯
  • ¥100 python读取速度问题
  • ¥15 stm32f407使用DMA问题
  • ¥15 您好 这个API接口该怎么弄 网站搭建好了 API也有 现在就不知道该怎么填写API 不知道怎么用
  • ¥88 用uniapp写一个多端的程序,用到高德地图,用高德的JSAPI吗?
  • ¥20 关于#c++#的问题:水果店管理系统
  • ¥30 dbLinq最新版linq sqlite
  • ¥20 对D盘进行分盘之前没有将visual studio2022卸载掉,现在该如何下载回来
  • ¥15 完成虚拟机环境配置,还有安装kettle
  • ¥15 有人会搭建生鲜配送自营+平台的管理系统吗