简体   繁体   English

将字符串时间戳转换为PHP中的时间戳

[英]Convert String Timestamp to timestamp in PHP

I have a string like "1427835599999" that I want to convert to timestamp so I can use it in the date() function. 我有一个类似“ 1427835599999”的字符串,我想将其转换为时间戳,以便可以在date()函数中使用它。

So far I have tried these: 到目前为止,我已经尝试了以下方法:

$time = date("Y-m-d", $timestamp);

which returns an error ($timestamp is expected to be a long, string given); 返回一个错误($ timestamp应该是一个很长的字符串);

$time = date("Y-m-d", strtotime($timestamp));

which returns 01-01-1970 返回01-01-1970

So I need to convert my string to long so I can use it in the date function. 因此,我需要将字符串转换为long,以便可以在date函数中使用它。

Thanks 谢谢

EDIT: 编辑:

(int)$timestamp is not working either. (int)$ timestamp也不起作用。 $timestamp+0 is not working. $ timestamp + 0无法正常工作。

It looks as a product of javascript's .getTime() function, returning number of milliseconds since 1970/01/01, hence 13 digits as opposed to timestamp being the number of seconds, so having 10 digits. 它看起来像是javascript .getTime()函数的.getTime() ,返回自1970/01/01以来的毫秒数,因此13位数(与timestamp相对)是秒数,因此具有10位数。 Just divide it by 1000: 只需将其除以1000:

$time = date("Y-m-d", $timestamp/1000);
echo $time; // output: 2015-03-31

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

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