Results 1 to 8 of 8

Thread: strtotime, too future date?

  1. #1
    Join Date
    Feb 2008
    Location
    Coventry
    Posts
    103
    Thanks
    5
    Thanked 8 Times in 8 Posts

    Question strtotime, too future date?

    hi guys, just wondering if this is me or not.

    PHP Code:
    <?
    $to_date 
    '23/09/2008';
    $to strtotime($to_date);
    echo 
    $to;
    ?>
    if i try and convert a string to time that has a date greater than 5 days in the future then it doesnt seem to work.
    if anybody can try it, they'll see, or they should see. unless im doing something wrong???
    The important thing is not to stop questioning. Curiosity has its own reason for existing.

  2. #2
    Join Date
    Feb 2006
    Posts
    236
    Thanks
    8
    Thanked 3 Times in 3 Posts

    Default

    Here is a little script that I have tucked away but unused - got it from here: http://www.phpclasses.org/browse/fil...html&no_cj_c=0
    The class is:
    Code:
    <?php
    
    
    
      /* ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
    
      Author : Carl Friis-Hansen
      Date   : June 21st 2008
      License: Freeware
    
      ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** */
    
    
    
      interface iunixDate {
    
        public  function  __construct( $utc=false );
                            //  Plain construction of object
    
        public  function  date( $param="now", $format="%Y/%0m/%0d - %0k:%0M - %Z" );
                            //  Function returnes the result of
                            //  the $param and $format supplied.
                            //  Default: Current date and time
                            //  like 2009/06/23 - 13:20 - CEST
    
        /* ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
    
        This class uses the great power of the standard UNIX/Linux/OSX function
        called "date", thus this class will only work for web servers hosted on
        one of these platforms and assuming this web server has the right to
        perform such a task. You can try this to see if it works:
          $date = new unixDate;
          print "  Date and time right now is ".$date->date()."  ";
        The advantage of using the operating systems standard commands is that
        these commands are tested extremely well and are updated in case of change
        in the world order, time zones, etc.
        Secondly date is very human like to use:
          $date = new unixDate;
          $result = $date->date( "2005-12-15 +1 year +2 days +5 hours +30 minutes" );
        Will put "2006/12/17 - 05:30 - CET" in $result for central Europe.
          $date = new unixDate;
          $result = $date->date( "now +1 year +2 days +5 hours +30 minutes" );
        Will put the current date/time plus 1 year 2 days 5 hours and 30 minutes
        in $result.
        Below is a list of the output format operators:
          %%   a literal %
          %a   locale's abbreviated weekday name (e.g., Sun)
          %A   locale's full weekday name (e.g., Sunday)
          %b   locale's abbreviated month name (e.g., Jan)
          %B   locale's full month name (e.g., January)
          %c   locale's date and time (e.g., Thu Mar  3 23:05:25 2005)
          %C   century; like %Y, except omit last two digits (e.g., 21)
          %d   day of month (e.g, 01)
          %D   date; same as %m/%d/%y
          %e   day of month, space padded; same as %_d
          %F   full date; same as %Y-%m-%d
          %g   last two digits of year of ISO week number (see %G)
          %G   year of ISO week number (see %V); normally useful only with %V
          %h   same as %b
          %H   hour (00..23)
          %I   hour (01..12)
          %j   day of year (001..366)
          %k   hour ( 0..23)
          %l   hour ( 1..12)
          %m   month (01..12)
          %M   minute (00..59)
          %n   a newline
          %N   nanoseconds (000000000..999999999)
          %p   locale's equivalent of either AM or PM; blank if not known
          %P   like %p, but lower case
          %r   locale's 12-hour clock time (e.g., 11:11:04 PM)
          %R   24-hour hour and minute; same as %H:%M
          %s   seconds since 1970-01-01 00:00:00 UTC
          %S   second (00..60)
          %t   a tab
          %T   time; same as %H:%M:%S
          %u   day of week (1..7); 1 is Monday
          %U   week number of year, with Sunday as first day of week (00..53)
          %V   ISO week number, with Monday as first day of week (01..53)
          %w   day of week (0..6); 0 is Sunday
          %W   week number of year, with Monday as first day of week (00..53)
          %x   locale's date representation (e.g., 12/31/99)
          %X   locale's time representation (e.g., 23:13:48)
          %y   last two digits of year (00..99)
          %Y   year
          %z   +hhmm numeric timezone (e.g., -0400)
          %:z  +hh:mm numeric timezone (e.g., -04:00)
          %::z  +hh:mm:ss numeric time zone (e.g., -04:00:00)
          %:::z  numeric time zone with : to necessary precision (e.g., -04, +05:30)
          %Z   alphabetic time zone abbreviation (e.g., EDT)
        For futher reading please use Google around for something like:
          +linux +date calculate
        ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** */
    
      } //  interface iunixDate
    
    
    
      class unixDate implements iunixDate {
    
        protected $utc;
    
        public  function  __construct( $utc=false ) {
          $this->utc = $utc;
        }
    
        public  function  date( $param="now", $format="%Y/%0m/%0d - %0k:%0M - %Z" ) {
          if( $this->utc ) {
            $utcopt = "-u ";
          } else {
            $utcopt = "";
          }
          return shell_exec( "date ".$utcopt."-d \"".escapeshellcmd( $param )."\" +\"".escapeshellcmd( $format )."\"" );
        } //  date(...)
    
      } //  class unixDate
    
    
    ?>
    And the usage is this:
    Code:
    <?php
    
    
    
      /* ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
    
      Simple example using web interface.
      Should output something like:
    
      The future: 2009/06/23 - 15:00 - CET
    
      ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** */
    
      require_once  "./class.unixDate.php";
    
      $date = new unixDate;
      $result1 = $date->date();
      $result2 = $date->date(  "now +1 year +2 days +5 hours +30 minutes",
                              "%Y/%0m/%0d - %0k:%0M - %Z"               );
    
      print "<html><head><title>Check Date</title></head><body>\n";
      print "<h2>Checking class.unixDate.php</h2>\n";
      print "------------------------------------------------------<br />\n";
      print "The future: ".$result2."<br />\n";
      print "is right now plus 1 year 2 days 5 hours and 30 minutes.<br />\n";
      print "Right now is by the way ".$result1."<br />\n";
      print "------------------------------------------------------<br />\n";
      print "2008/12/15 +30 days = ".
            $date->date(  "2008/12/15 +30 days", "%Y/%0m/%0d" ).
            "<br />\n";
      print "------------------------------------------------------<br />\n";
      print "2009/01/14 -30 days = ".
            $date->date(  "2009/01/14 -30 days", "%Y/%0m/%0d" ).
            "<br />\n";
      print "------------------------------------------------------<br />\n";
      print "</body></html>";
    
    ?>

  3. #3
    Join Date
    Feb 2008
    Location
    Coventry
    Posts
    103
    Thanks
    5
    Thanked 8 Times in 8 Posts

    Default

    wow dude that lost me, i know a bit of php but that just blew me away. any chance you could talk it through?

    cheers
    The important thing is not to stop questioning. Curiosity has its own reason for existing.

  4. #4
    Join Date
    Feb 2006
    Posts
    236
    Thanks
    8
    Thanked 3 Times in 3 Posts

    Default

    A simple method taken from the PHP: date man page is:
    Code:
    <?php
    $tomorrow  = mktime(0, 0, 0, date("m")  , date("d")+1, date("Y"));
    $lastmonth = mktime(0, 0, 0, date("m")-1, date("d"),   date("Y"));
    $nextyear  = mktime(0, 0, 0, date("m"),   date("d"),   date("Y")+1);
    ?>
    But, it doesn't take in daylight savings time, and not good when spanning time zones.

    HTH

  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Your problem is that PHP's date handling isn't very smart, and it seems to default to the ridiculous American MM/DD/YYYY format, possibly the most illogical date format in the world, and doesn't fall back properly onto the date format used in half the rest of the world. If you use an ISO international YYYY-MM-DD, all is fine.

    Don't be scared by the class, my initial reaction was 'OVERKILL!' and then I realised that 4/5 of the code was a comment. It's not necessary in order to solve your problem, though.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. #6
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default

    Dear city_coder:

    I'm not 100% sure I understand what you are trying to do, but if you want to display the final date as dd/mm/yyyy, then input the $to_date using dashes instead of slashes and then convert it like so:

    Code:
    <?php
    $to_date = '30-11-2008'; 
    $to = date('d/m/Y', strtotime($to_date)); 
    echo $to; 
    ?>
    Works just fine for me no matter what the date. Would this work for your application?
    e

  7. #7
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Good job, kuau: that does indeed seem to work too

    Probably ISO is best for internal use in applications, though.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  8. #8
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default

    Thanks, Twey

Bookmarks

Posting Permissions

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