PHP Dates and Datetimes from ISO Dates

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Keith Hughitt

    PHP Dates and Datetimes from ISO Dates

    Hi all,

    I was wondering if someone could help me out with a question about PHP
    Dates. I would like to pass a date to PHP via a query string, using
    ISO 8601 formatting ("2008-09-15T00:00:00Z"). I then would like to be
    able to manipulate the date in PHP, and do things like printing out
    the hour, adding seconds, etc. So far the best I've come up with is
    use PHP's "strptime" function to get an array of the date components:

    $ts = "2008-09-15T00:00:00Z"
    $format = '%Y-%m-%dUTC%H:%M:%S';
    $d = strptime(date($ ts), $format);
    $mon = str_pad($d['tm_mon'] + 1, 2 , "0", STR_PAD_LEFT);
    etc..

    Can anyone tell me a better way of working with dates in PHP? I've dug
    around in the manual and found several objects (Date, Datetime, etc),
    but am not sure which is best suited for what kinds of jobs, or how to
    even get from an ISO date to a Datetime object for instance.

    Any suggestions would be greatly appreciated.

    Thanks!
    Keith
  • Michael Fesser

    #2
    Re: PHP Dates and Datetimes from ISO Dates

    ..oO(Keith Hughitt)
    >I was wondering if someone could help me out with a question about PHP
    >Dates. I would like to pass a date to PHP via a query string, using
    >ISO 8601 formatting ("2008-09-15T00:00:00Z"). I then would like to be
    >able to manipulate the date in PHP, and do things like printing out
    >the hour, adding seconds, etc. So far the best I've come up with is
    >use PHP's "strptime" function to get an array of the date components:
    >
    >$ts = "2008-09-15T00:00:00Z"
    >$format = '%Y-%m-%dUTC%H:%M:%S';
    >$d = strptime(date($ ts), $format);
    >$mon = str_pad($d['tm_mon'] + 1, 2 , "0", STR_PAD_LEFT);
    >etc..
    date_parse() should give you all you need here:

    <?php
    $ts = "2008-09-15T00:00:00Z";
    var_dump(date_p arse($ts));
    ?>

    For formatted output you don't need str_pad(), but could use printf()
    instead if you want to print out leading zeros.

    You could also work with the DateTime class, but would have to search
    the Web for more details, because the PHP docs on this class are very
    sparse.

    <?php
    $ts = "2008-09-15T00:00:00Z";
    $date = new DateTime($ts);
    ....
    ?>

    Micha

    Comment

    • Keith Hughitt

      #3
      Re: PHP Dates and Datetimes from ISO Dates

      On Sep 15, 9:40 am, Michael Fesser <neti...@gmx.de wrote:
      .oO(Keith Hughitt)
      >
      I was wondering if someone could help me out with a question about PHP
      Dates. I would like to pass a date to PHP via a query string, using
      ISO 8601 formatting ("2008-09-15T00:00:00Z"). I then would like to be
      able to manipulate the date in PHP, and do things like printing out
      the hour, adding seconds, etc. So far the best I've come up with is
      use PHP's "strptime" function to get an array of the date components:
      >
      $ts = "2008-09-15T00:00:00Z"
      $format = '%Y-%m-%dUTC%H:%M:%S';
      $d = strptime(date($ ts), $format);
      $mon  = str_pad($d['tm_mon'] + 1, 2 , "0", STR_PAD_LEFT);
      etc..
      >
      date_parse() should give you all you need here:
      >
      <?php
      $ts = "2008-09-15T00:00:00Z";
      var_dump(date_p arse($ts));
      ?>
      >
      For formatted output you don't need str_pad(), but could use printf()
      instead if you want to print out leading zeros.
      >
      You could also work with the DateTime class, but would have to search
      the Web for more details, because the PHP docs on this class are very
      sparse.
      >
      <?php
      $ts = "2008-09-15T00:00:00Z";
      $date = new DateTime($ts);
      ...
      ?>
      >
      Micha
      Hi Michael,

      Thanks for the explanation. That is much simpler :)

      I'm starting to think, however, that It would be better just to pass
      in a Unix timestamp. I hadn't considered it
      before, but it is very simple to go from a JavaScript date to a Unix
      timestamp.

      I appreciate the help.

      Take care,
      Keith

      Comment

      • Michael Fesser

        #4
        Re: PHP Dates and Datetimes from ISO Dates

        ..oO(Keith Hughitt)
        >Thanks for the explanation. That is much simpler :)
        >
        >I'm starting to think, however, that It would be better just to pass
        >in a Unix timestamp. I hadn't considered it
        >before, but it is very simple to go from a JavaScript date to a Unix
        >timestamp.
        >
        >I appreciate the help.
        You're welcome.

        Micha

        Comment

        Working...