By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,278 Members | 1,342 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,278 IT Pros & Developers. It's quick & easy.

PHP Dates and Datetimes from ISO Dates

P: n/a
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
Sep 15 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
..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_parse($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
Sep 15 '08 #2

P: n/a
On Sep 15, 9:40*am, Michael Fesser <neti...@gmx.dewrote:
.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_parse($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
Sep 15 '08 #3

P: n/a
..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
Sep 15 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.