473,385 Members | 1,898 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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
Sep 15 '08 #1
3 3061
..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
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
..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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jesse O | last post by:
I have two date fields, start_date and end_date. I'd like to subtract the two dates, and come up with a number (the number of difference between the two dates). What function is there to do...
2
by: matthias s. | last post by:
Hi there, I'm surely not the first one to have this problem. But searching the group didn't yield any results, so here goes: When storing DateTime values in an SQL Database (using a 'datetime'...
13
by: tshad | last post by:
I have a dataset with a date in it. The type is smalldatetime on Sql Server and I am just doing a "SELECT * from x" to get it. All my other fields are fine, but this one gives me an error of: ...
6
by: Jordan | last post by:
I'm working with a Repeater using a custom function inside the <itemtemplate> to return a text string. The parameters SHOULD be (DateTime, DateTime, Int16) but the difficulties in getting DateTimes...
1
by: Manny Chohan | last post by:
i have two date fields for event start date and end date. I let the user enter two on create event page. on submit, i need to compare if the end date is earlier than start date or not. How can i...
6
by: Simon Harvey | last post by:
Hi everyone, I need to be able to compare to dates to ensure that one is at least 1 day greater than the other. Im trying to do if(toDate !> fromDate){ // handle }
0
by: Claes at work | last post by:
Hi, I am learning Python and want to perform what I think is a very simple task: calculating the difference between two dates in seconds. Reading through the documentation I am puzzled: I can't...
5
by: Mike | last post by:
I use c#, V2005 How I can get difference between two dates and get value in month(s) I had found some solutions but it is not exactly what I need. private static int...
2
by: dantebothermy | last post by:
Is there a simple way to subtract the time from a datetime field, so when I compare dates, I'm always comparing the dates at 12:00 am? I'd really prefer not to convert all my dates to strings. ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.