[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

strtotime mismatch in php

Posted on 2014-09-02
2
Medium Priority
?
364 Views
Last Modified: 2014-11-12
Hello

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?
0
Comment
Question by:itnifl
2 Comments
 
LVL 58

Accepted Solution

by:
Gary earned 1400 total points
ID: 40298512
<?php
$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
http://php.net/manual/en/datetime.format.php
0
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 600 total points
ID: 40298553
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.
http://iconoun.com/demo/strtotime.php?s=1492-10-12T00%3A00%3A00%2B00%3A00 

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.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html
0

Featured Post

Automating Your MSP Business

The road to profitability.
Delivering superior services is key to ensuring customer satisfaction and the consequent long-term relationships that enable MSPs to lock in predictable, recurring revenue. What's the best way to deliver superior service? One word: automation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As managed cloud service providers, we often get asked to intervene when cloud deployments go awry. Attracted by apparent ease-of-use, flexibility and low computing costs, companies quickly adopt leading public cloud platforms such as Amazon Web Ser…
If anyone asked you to network diagram of the internet, it was drawn in the form of a fluffy cloud which further became known as cloud computing. Popularly cloud computing is defined as workloads that run over the internet in a commercial provider’s…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses
Course of the Month20 days, 4 hours left to enroll

873 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