Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts

    Javascript strtotime.

    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?
    Last edited by DJCMBear; 12-17-2011 at 11:03 PM.
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P

  • #2
    Gütkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    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?
    .My new Javascript tutorial site: http://reallifejs.com/
    .Latest article: Calculators — Tiny jQuery calculator, Full-fledged OOP calculator, Big number calculator
    .Latest quick-bit: Including jQuery — Environment-aware minification and CDNs with local fallback

  • #3
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    I tested it in Windows Safari 5.1.2 and it wouldn't work it would just return false, i used this.

    Code:
    alert(strtotime('18-12-2011')); // returned false
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P

  • #4
    Gütkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    It seems the phpjs implementation of strtotime does a lot less than PHP's strtotime does.

    This regexp
    PHP Code:
    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.
    .My new Javascript tutorial site: http://reallifejs.com/
    .Latest article: Calculators — Tiny jQuery calculator, Full-fledged OOP calculator, Big number calculator
    .Latest quick-bit: Including jQuery — Environment-aware minification and CDNs with local fallback

  • #5
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    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.

    Code:
    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.
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P

  • #6
    Gütkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    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:

    Code:
    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'));
    .My new Javascript tutorial site: http://reallifejs.com/
    .Latest article: Calculators — Tiny jQuery calculator, Full-fledged OOP calculator, Big number calculator
    .Latest quick-bit: Including jQuery — Environment-aware minification and CDNs with local fallback

  • #7
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    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.
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P

  • #8
    Gütkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    Quote Originally Posted by DJCMBear View Post
    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:

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

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

    alert(date1);
    alert(date2); 
    .My new Javascript tutorial site: http://reallifejs.com/
    .Latest article: Calculators — Tiny jQuery calculator, Full-fledged OOP calculator, Big number calculator
    .Latest quick-bit: Including jQuery — Environment-aware minification and CDNs with local fallback

  • #9
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    I have managed to fix it and it now does what I need it to do, thank you for all your help
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •