PHP date format conversion - php

Need help in date conversion:
Existing date format:
Wed, 01 Dec 2010 00:00:00 -0500
(Day, DD MMM YYYY HH:MM:SS GMT-0500)
To be changed to:
2010-11-29T04:59:59-05:00
(YYYY-MM-DD(T)HH:MM:SS GMT-05:00)
How to handle in PHP?
is there any function available in php for this.
please help

strtotime() (man page) & date() (man page) or DateTime class (man page) should be able to handle this.
echo date('Y-m-d\TH:i:sZ', strtotime('Wed, 01 Dec 2010 00:00:00 -0500'));
echo date('c', strtotime('Wed, 01 Dec 2010 00:00:00 -0500')); // as mentioned by Anthony
or
echo DateTime('Wed, 01 Dec 2010 00:00:00 -0500')->format('Y-m-d\TH:i:sZ');
echo DateTime('Wed, 01 Dec 2010 00:00:00 -0500')->format('c'); // as mentioned by Anthony

First you want the date string in epoch format, so that you can use the date function. My favorite method to do this is the strtotime function:
$epoch_date = strtotime($original_date_string);
Now, you can use the date function to output it however you like. In your case, I believe you are looking for ISO 8601, which is built into the function:
$new_date_string = date('c', $epoch_date);
echo $new_date_string;

date('Y\-m\-d\Th:i:s \G\M\TP');
This will return:
2010-11-26T02:49:24 GMT-05:00
Use the date() formating its much simpler!
You can read all about it right here http://php.net/manual/en/function.date.php

Related

Converting timestamps in php

I got time stamp in this format from twitter api.
Fri Dec 28 20:06:38 +0000 2012
I want to convert this to standard time stamp format like this one.
2012-12-10 16:20:18
Am pretty new to dates in php. How can I do it??
You can use DateTime:
$date = new DateTime('Fri Dec 28 20:06:38 +0000 2012');
echo $date->format('Y-m-d H:i:s');
The reason why I prefer DateTime is that it gives great oop implementation and makes it easier to work with dates that are quite big head-ache of programmer. For more information about this class read the manual that I have already referenced.
You can use strtotime to convert the initial string to a UNIX timestamp and then strftime to convert it back to a string:
strftime('%Y-%m-%d %H:%M:%S', strtotime('Fri Dec 28 20:06:38 +0000 2012'));
That call returns the string 2012-12-28 21:06:38 - i.e. exactly what you are looking for.
use this
$originalDate = "Fri Dec 28 20:06:38 +0000 2012";
echo $newDate = date("Y-m-d h:i:s", strtotime($originalDate));
working example http://codepad.viper-7.com/AhLcEU
In case of use php < 5.2.0 (some shared hostings) use a combination of strtotime and date
date('Y-m-d H:i:s', strtotime('Fri Dec 28 20:06:38 +0000 2012'));

strtotime fails on pubDates from RSS feeds?

Example:
print strtotime('Sat, 03 Nov 2012 20:17:12 0000');
prints nothing :(
And the date string appears to be correct..
it's:
day name, day month name year hour:min:sec timezone
Valid formats are explained in Date and Time Formats.
Consider using DateTime objects, and the createFromFormat() method
I get it you want to add a timezone correction. But the timezone requires a sign.
print strtotime('Sat, 03 Nov 2012 20:17:12 +0000');
This will work
print strtotime('03 Nov 2012 20:17:12');
Output: 1351988232
This works perfectly. You should remove the 0000 and the day (Sat)

Change format of date with PHP

I want to change this date format: "Tue Apr 3 15:00:03 GMT+0300 2012" to "3.4.2012" with PHP. Is it possible?
I tried:
$date="Tue Apr 3 15:00:03 GMT+0300 2012";
echo date('d.m.Y', strtotime($date));
but it results in: 03.04.2015. What am I doing wrong?
Read the Manual before using a function, and especially before asking other people to read it for you, like I just did to answer your question:
date('j.n.Y', strtotime($date));
Codepad Example
Your date is not formatted correctly, at least it is not the RFC 2822 date format,
Tue, 3 Apr 2012 15:00:03 +0300
strtotime is quite flexible but it cannot guess what you mean ...

Convert a human date to unix timestamp

I have a jQuery script that returns a date as:
Wed Nov 09 2011 16:30:00 GMT-0700 (MST)
How could I convert that into unix timestamp? I was looking at mktime() but I'm not really understanding it completely. Any ideas?
If you're using PHP 5.2, try the DateTime class, eg
$dt = new DateTime("Wed Nov 09 2011 16:30:00 GMT-0700 (MST)");
$ts = $dt->getTimestamp();
Otherwise, try strtotime(), eg
$ts = strtotime("Wed Nov 09 2011 16:30:00 GMT-0700 (MST)");
echo date("r", $ts);
For me, this outputs
Thu, 10 Nov 2011 10:30:00 +1100
Note that the date() function is local timezone aware
I take it that jQuery's using a Date object; instead, have the script send the value of Math.floor(theDate.getTime() / 1000) to your PHP script. That's the Unix timestamp you need.
What about strtotime ?
$test = strtotime('Wed Nov 09 2011 16:30:00 GMT-0700 (MST)');
echo $test;
output : 1320881400

the equivalent of new Date(2011, 3, 1) in PHP

What is the equivalent of javascript new Date(2011, 3, 1) in PHP?
p.s. the output of the function above is:
Fri Apr 01 2011 00:00:00 GMT+0100 (BST) {}
I have tried looking trough php DateTime Predefined Constants, but didn't find similar patern.
Try using this:
echo date ("M-d-Y", mktime (0,0,0,12,32,1997));
More information can be found here: mktime function manual
You just have to use proper values in date("XXX") function. More info can be found here: date function manual
If you prefer Greenwich Mean Time (GMT) you should use: gmdate
<?php
$date = date_create('2011-03-01');
echo date_format($date, 'D M d Y H:i:s eO (T)');
?>
Formatting: http://www.php.net/manual/en/function.date.php
If you want the RFC2822 standardized formatting, use r for the format. That would give you the format:
Thu, 21 Dec 2000 16:01:07 +0200
<?php
date("D M d Y H:i:s \G\M\TO (T)",strtotime('Midnight April 1st 2011'));
?>
Which would output Fri Apr 01 2011 00:00:00 GMT+0100 (BST) if your timezone is set correctly (in this case to Europe/London)
Or use mktime() or time() or a timestamp or what have you instead of strtotime(). Note that eO (T) in the date format won't give the same output as the java function, as if the timezone is North America/New York or whatever, then it would output something like North America/New York-0500 (EST) Rather than GMT-0500 (EST)
Can also use a DateTime object.

Categories