View Full Version : Resolved Javascript strtotime.

12-17-2011, 05:58 PM
Hey for some reason I am not able to use this script (http://phpjs.org/functions/strtotime:554) in Mac Safari, how can I make it work in Safari as well as all the other browsers, as Safari is the only one which doesn't seem to work?

12-17-2011, 07:17 PM
Seems to work in Windows Safari could you please confirm that this is a Mac only problem (in which case I won't be able to help), or, if it's not, provide a test case with expected and actual result?

12-17-2011, 07:19 PM
I tested it in Windows Safari 5.1.2 and it wouldn't work it would just return false, i used this.

alert(strtotime('18-12-2011')); // returned false

12-17-2011, 08:11 PM
It seems the phpjs implementation of strtotime does a lot less than PHP's strtotime does.

This regexp

match = str.match(/^(\d{2,4}-\d{2}-\d{2})(?:\s(\d{1,2}:\d{2}(:\d{2})?)?(?:\.(\d+))?)?$/);

obviously doesn't like DD-MM-YYYY, so you could either switch to YYYY-MM-DD, or change the regexp, or apply some logic to the string beforehand, so it will fit the regexp.

Generally, I'd say if you're not doing anything more complex than parsing strings containing explicit dates, you probably shouldn't use phpjs's strtotime at all, since it doesn't do much of what you'd expect it to do (those four test cases in the source are rather meager), and its use of Date.parse generates cross browser issues.

12-17-2011, 08:19 PM
Shockingly I haven't every needed to do things like this before so never needed to, basically I want to be able to turn something like '+1 year 2 months 5 days' etc like strtotime would do but also allow it to have is turn 12-12-2011 to time as well so I can use it within the javascripts Date function.

Something like this.

function limit (date, limit) {
var callback, now = new Date();
if (date) { now = date; }
now = strtotime(now);
callback = limit ? strtotime(limit, now) : now;
return new Date(callback);

alert(limit('12-12-2011', '+2 days')); // returns 14-12-2011
alert(limit('12-12-2011', '+13 months')); // returns 12-01-2013

Ruff code so not 100% correctly done.

12-17-2011, 09:08 PM
A major problem with your limit function (apart from being overly complicated) is that Date(callback) won't work the way you expect, because Javascript's Date constructor takes milliseconds, and not seconds.

As for the date format: If the date strings are not coming from some other data source, just switch to a standard format like MM-DD-YYYY or YYYY-MM-DD, which can be parsed by Javascript's Date itself (which is what phpjs's strtotime tries to do anyway at first).

If your DD-MM-YYYY format is coming from some other data source, just change it to a standard format inside your limit function, so you won't have to monkeypatch strtotime.

This could look like this:

function limit(date, limit) {
if (date.match(/^\d{2}-\d{2}-\d{4}$/)) {
date = date.split('-').reverse().join('-');
return new Date((limit ? strtotime(limit, strtotime(date)) : strtotime(date)) * 1000);

alert(limit('18-12-2011', '+2 days'));
alert(limit('18-12-2011', '+13 months'));

12-17-2011, 09:11 PM
How can I get this to work without using phpjs's strtotime function?

Also I did say it wasn't 100% as I couldn't spend much time on it at the time of writing it.

12-17-2011, 09:58 PM
How can I get this to work without using phpjs's strtotime function?

There's no way of answering that question without knowing where those strings are coming from, and whether their format is fixed or just some choice you made for no particular reason. If this is just about adding time to a date, and not about parsing time spans, it's trivial:

var date1 = new Date('2011-12-18');
date1.setDate(date1.getDate() + 2);

var date2 = new Date('2011-12-18');
date2.setMonth(date2.getMonth() + 13);


12-17-2011, 11:04 PM
I have managed to fix it and it now does what I need it to do, thank you for all your help :)