1

I am getting different time conversions while using a datetimepicker in js and converting the date in php

My PHP code is:

<?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

<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/

3 Answers 3

1

Thats because strtotime does not support your format ('5/29/2013 00:00PM');

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

Sign up to request clarification or add additional context in comments.

4 Comments

So how do I convert the string in strtotime compatible in php?
as i mentioned you can use php.net/manual/en/datetime.createfromformat.php this method
Like $d=DateTime::createFromFormat('m/d/Y H:iA','5/29/2013 0:00:PM'); echo $d->format("Y-m-d H:i:s"); Great Thing
It is not true that strtotime does not support the format mm/dd/y hh:MMmeridian; it's just that the values have to be in the licit ranges.
1

The time '00:00PM' is malformed; as documented, an hour hh with meridian has the format "0"?[1-9] | "1"[0-2], i. e. not zero, but one to twelve.

Comments

0

This extract from the PHP manual might be relevant:

"Dates in the m/d/y or d-m-y 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 d-m-y format is assumed. "

(https://www.php.net/manual/en/function.strtotime.php)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.