简体   繁体   English

javascript和php中的日期时间转换

[英]Date time conversion in javascript and php

I am getting different time conversions while using a datetimepicker in js and converting the date in php js使用datetimepicker并在php转换date ,我得到了不同的time conversions

My PHP code is: 我的PHP代码是:

<?php
    date_default_timezone_set('Asia/Calcutta');
    echo date('Y-m-d H:i:s A',strtotime('5/29/2013 00:00PM'));
    //outputs 1970-01-01 05:30:00 AM
    //echo date('Y-m-d H:i:s A',strtotime('5/29/2013 0:00AM'));//same result
    //tested on http://writecodeonline.com/php/
?>

And js js

<script>
    var s='5/29/2013 0:00PM';
    parseDate= function(s) {
        var currentYear, currentMonth, currentDay, currentHour, currentMinute;
        arr=s.split(' ');
        dateArr=arr[0].split('/');
        currentYear=dateArr[2];
        currentMonth=parseInt(dateArr[0])-1;
        currentDay=dateArr[1];
        timeArr=arr[1].split(':');
        currentHour=timeArr[0];

        if(s.contains('AM')===false)
        {
            currentHour=parseInt(timeArr[0])+12;
            currentMinute=parseInt(timeArr[1].replace('PM'));
        }
        else
        {
            currentMinute=parseInt(timeArr[1].replace('AM'));
        }
        console.log(currentYear,currentMonth,currentDay,currentHour,currentMinute,0,s.contains('AM'));
        return new Date(currentYear,currentMonth,currentDay,currentHour,currentMinute,0);

    }
    console.log(new Date());
    alert(parseDate(s));
    //outputs Wed May 29 2013 12:00:00 GMT+0530 (India Standard Time)
</script>

Fiddle http://jsfiddle.net/36vjH/ 小提琴http://jsfiddle.net/36vjH/

Thats because strtotime does not support your format ('5/29/2013 00:00PM'); 那是因为strtotime不支持您的格式('5/29/2013 00:00 PM');

Try: strtotime('2013-5-29 00:00:00); 试试:strtotime('2013-5-29 00:00:00); or http://www.php.net/manual/en/datetime.createfromformat.php instead. 或改为使用http://www.php.net/manual/en/datetime.createfromformat.php

The time '00:00PM' is malformed; 时间“ 00:00 PM”格式错误; as documented, an hour hh with meridian has the format "0"?[1-9] | "1"[0-2] 如记录,一个小时hh与子午线具有格式"0"?[1-9] | "1"[0-2] "0"?[1-9] | "1"[0-2] , ie not zero, but one to twelve. "0"?[1-9] | "1"[0-2] ,即不是零,而是一到十二。

This extract from the PHP manual might be relevant: PHP手册中的以下摘录可能是相关的:

" Dates in the m/d/y or dmy formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European dmy format is assumed. " 通过查看各个组成部分之间的分隔符,可以消除m / d / y或dmy格式的日期的歧义:如果分隔符为斜杠(/),则假定为美国m / d / y;而如果分隔符为破折号(-)或点(。),则采用欧洲dmy格式。

( http://uk3.php.net/manual/en/function.strtotime.php ) http://uk3.php.net/manual/zh/function.strtotime.php

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

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