strtotime mismatch in php

Posted on 2014-09-02
Last Modified: 2014-11-12

This code ends up in two ways depending in what server it is run:
date("H:i:s d-m-Y", strtotime('1492-10-12T00:00:00+00:00'))
WebServer 1(correct result): 00:00:00 12-10-1492
WebSite in Azure: 16:00:00 31-12-1969

I have no idea why this is happening. Does it have something to do with the php setup being different on the two servers, or maybe timezones or dateformat being different on the servers? How do I fix this so that I get the same result on both servers?
Question by:itnifl
    LVL 58

    Accepted Solution

    $date = new DateTime('1492-10-12T00:00:00+00:00');
    echo $date->format('H:i:s d-m-Y');

    Open in new window

    strtotime only works for dates from 1970 on Windows
    LVL 107

    Assisted Solution

    by:Ray Paseur
    This sort of date value ("31-12-1969") is almost always the signature of the failure of strtotime(), accompanied by a programming error.  It happens because when strtotime() fails, it returns FALSE.  The programming error happens because the programmer assumed that strtotime() always worked, did not test for FALSE in the script, and used the output from strtotime().  PHP is a loosely typed language, so the value of FALSE, when fed to the date() function, was turned into zero.

    On the current levels of 64-bit PHP, this works just fine.  

    From the man page: "For 64-bit versions of PHP, the valid range of a timestamp is effectively infinite, as 64 bits can represent approximately 293 billion years in either direction."

    You might also find this article helpful.

    Featured Post

    Will my email signature work in Office 365?

    You've built an email signature using raw HTML code in Office 365, but you can't review how it looks with Transport Rules. So you have to test it over and over again before it can be used. Isn't this a bit of a waste of your time? Wouldn't a WYSIWYG editor make it a lot easier?

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    php explode 3 28
    Amazon S3 Security 2 28
    Summernote required 3 21
    CSRF session variables 24 22
    Healthcare providers, insurance companies and other covered entities trust eFax Corporate to transmit their most sensitive documents. eFax Corporate can help your organization implement a HIPAA compliant cloud faxing solution.
    Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
    Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now