I have a date format like this 'Tue May 01 00:00:00 +1000 2012' (from array data on json file)
when I use date() function it returning April, :D
echo date('F Y', strtotime('Tue May 01 00:00:00 +1000 2012'));
//it returning "April 2012"
Any ideas how to fix this?
Many Thanks!
Instead of using the old date/time functions that mess things up because they implicitly involve your local time zone, use DateTime:
$date = new DateTime('Tue May 01 00:00:00 +1000 2012');
echo $date->format('F Y');
This will also work corrrectly for any date, regardless of the timezone (UTC+10 hours or anything else).
You will need to check both the timezones. Or set a custom one for both date and strtotime calls.
Since you are using +1000 as your timezone-offset; I'm assuming it is Australia. You can use the date_default_timezone_set() call to set timezone to Australia.
echo date('F Y', strtotime('Tue May 01 00:00:00 +1000 2012'));
date_default_timezone_set('Australia/Queensland');
echo date('F Y', strtotime('Tue May 01 00:00:00 +1000 2012'));
Here is the codepad link: http://codepad.org/tPC8DEQp
Related
I have this in my html for a result
<?php echo $row['end_date']?>
that results in 2014-01-31 22:00:00
I want it to echo this format
01/31/14 10:00pm OR January 31, 2014 10pm if possible
Any help will be much appreciated.
The DateTime class is incredible for this.
You just need to create a DateTime object with the string that MySQL returns, and it will parse it automatically. Then, just call the DateTime::format method to format it the way you want.
<?php
$date = new DateTime($row['end_date']); // e.g. 2014-01-31 22:00:00
echo $date->format(DateTime::RFC2822); // result: Fri, 31 Jan 2014 22:00:00 +0100
echo $date->format('F j, Y g:ia'); // result: January 31, 2014 10:00pm
?>
For formatting the date, see the manual for the date() function.
There are many ways to do this.
http://in2.php.net/strtotime
Many other options are available to format the date the way you want.
Thanks
Amit
Through the twitter API I was able to get the datetime of when something was tweeted
Jul 25 17:42:55 +0000 2013
Now, in PHP, how do I get that into standard unix:
2013-6-25 17:42:55
I'm not to sure on anything to deal with datetime but I think there is an easier way to do this rather than having to parse through and change things with str_replace and substr
Use the DateTime class, specifically the static createFromFormat() method
$dt = DateTime::createFromFormat('M j H:i:s P Y', 'Jul 25 17:42:55 +0000 2013');
echo $dt->format('Y-m-d H:i:s');
Working example - http://codepad.viper-7.com/gLdEll
Simply pass it through strtotime. Note that this includes a timezone +0000 so the time will translate relative to your timezone also.
<?php
$date = "Jul 25 17:42:55 +0000 2013";
echo date("Y-m-d H:i:s", strtotime($date));
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'));
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.
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