如何使用php解码JSON日期?
我从JSON输出中获得JSON加密中的日期,我想在将其插入mysql时对其进行解码 我插入输出:如何使用php解码JSON日期?,php,Php,我从JSON输出中获得JSON加密中的日期,我想在将其插入mysql时对其进行解码 我插入输出: "date": "/Date(1446739002960)/" 到$dateparse变量 我正在使用javascript编写解决方案: var dateString = "\/Date(753343200000)\/".substr(6); var currentTime = new Date(parseInt(dateString )); var month = currentTime.get
"date": "/Date(1446739002960)/"
到$dateparse变量
我正在使用javascript编写解决方案:
var dateString = "\/Date(753343200000)\/".substr(6);
var currentTime = new Date(parseInt(dateString ));
var month = currentTime.getMonth() + 1;
var day = currentTime.getDate();
var year = currentTime.getFullYear();
var date = day + "/" + month + "/" + year;
alert(date);
如何使用php解码可变内容?
谢谢你修复你的JSON
$dateJSON='{“日期”:“/date(1446739002960)/”}'
2)解码您的JSON
$timestamp=json\u decode($dateJSON,true)代码>
3)删除所有非数字字符
$timestamp=preg_replace(“/[^0-9]/”,“$timestamp['date'])
4)将时间戳(除以JS日期的1000原因)转换为人类可读的格式
$date=date(“Y-m-dh:i:s”,$timestamp/1000)代码>首先,您的json代码不完整,缺少需要过滤并通过json_解码和时间函数传递的花括号:
// Curly brackets where missing
$obj = json_decode( '{"date": "/Date(753343200000)/"}' );
// Remove non numerical characters
$obj->date = preg_replace( '/[^0-9]/', '', $obj->date );
// Divide javascript date (in ms) with 1000 to get UNIX date (seconds)
// Convert the date as DD-MM-YYYY
$dateparse = date( 'd/m/Y', ( $obj->date / 1000 ) );
echo $dateparse;
编辑更新日期并更正JavaScript日期
如果您也需要时间,请使用date('d/m/Y H:i:s',($obj->date/1000))
不要忘记时区,因为JavaScript日期字符串可能包含它,下面是如何为这两种情况解析它
// Let's assume you did JSON parsing and got your date string
$date = '/Date(1511431604000+0000)/';
// Parse the date to get timestamp and timezone if applicable
preg_match('/\/Date\(([0-9]+)(\+[0-9]+)?/', $date, $time);
// remove milliseconds from timestamp
$ts = $time[1] / 1000;
// Define Time Zone if exists
$tz = isset($time[2]) ? new DateTimeZone($time[2]) : null;
// Create a new date object from your timestamp
// note @ before timestamp
// and don't specify timezone here as it will be ignored anyway
$dt = new DateTime('@'.$ts);
// If you'd like to apply timezone for whatever reason
if ($tz) {
$dt->setTimezone($tz);
}
// Print your date
print $dt->format('Y-m-d H:i:s');
第一个问题是JSON日期的格式也可以是'/date(1511431604000+0000)/'
,因此如果存在+0000,我们需要忽略它,否则如果我们使用preg\u replace('/[^0-9]/','.$JSON\u date)
,则会将0000添加到时间戳中:
$json_date = '/Date(1511431604000+0000)/'; // or $json_date = '/Date(1511431604000)/';
// Parse date to get the timestamp and the timezone if set
preg_match('/\/Date\(([0-9]+)(\+[0-9]+)?/', $json_date, $time_array);
// Remove milliseconds from JSON timestamp to make it a Unix timestamp
$unix_timestamp = intval($time_array[1] / 1000);
您可以使用此包解析JSON日期:
您是否尝试了json解码($yourDate)
?$times=“/Date(1446739002960)/”$时间戳=json_解码($times)$日期=日期(“Y-m-d H:i:s”,$时间戳);echo$日期;最好接受哈斯·比约克的答案。您需要删除所有非数字字符。顺便说一句,时间戳可能是错的。您将得到类似于47815-05-03 10:05:00
您需要将JS日期除以1000才能得到PHP日期。。。并更改json_解码($dateJSON,true)代码>获取数组而不是对象。工作正常,但解码后获取的日期无效。。。请尝试753343200000,它必须是1995年11月15日请查看问题@HasseOk中的javascript代码,我将日期格式更改为您想要的格式dd/mm/yyyy,并修复了将JS日期转换为PHP日期时出现的错误。JavaScript以毫秒为单位,PHP以秒为单位。时间戳表示对秒或微秒时间的绝对引用。时区与此无关。将时间戳表示为本地时区中的字符串时,将使用时区。
use \Webapix\DotNetJsonDate\Date;
Date::toDateTime('/Date(1446739002960)/'); // return with \DateTime object