Formatting Date with PHP from weird twitter format - php

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));

Related

PHP date returning wrong value

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

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'));

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.

How can I get today's timestamp in PHP

I tried
$dtToday = DateTime::createFromFormat('Y-m-d', date('Y-m-d'));
but when I output it
die($dtToday->format('d M Y g:i:s a'));
I still get the time eg "22 Jan 2011 4:53:59 pm". Why is that?
UPDATE
Ah... many people misunderstood me, my bad, I forgot to point out the main point. I created the date with just the date portion, I don't want the time. So I'd expect something like
22 Jan 2011 12:00:00 am
You can call ->setTime(0, 0) to zero out the time portion:
$date = DateTime::createFromFormat('Y-m-d', '2011-01-22')->setTime(0, 0);
echo $date->format('d M Y g:i:s a');
// 22 Jan 2011 12:00:00 am
See the documentation for DateTime::createFromFormat:
If format does not contain the character ! then portions of the generated time which are not specified in format will be set to the current system time.
If you do the following function call, you'll get the result you expect:
$dtToday = DateTime::createFromFormat('!Y-m-d', date('Y-m-d'));
Today's start timestamp
$todayStartTS = strtotime(date('Y-m-d', time()) . ' 00:00:00');
You can do this by passing the current unix timestamp as the second parameter to the date function
echo date("Y-m-d H:i:s",time());
Remove this part g:i:s a from your code.
Now, if you want a nice date formatted according to your local, i recommand you to use strftime() function.
You are getting "22 Jan 2011 4:53:59 pm" because those are the rules you format your date with :
d (day) : 22
M (Month) : Jan
Y (Year) : 2011
g (12-hour format) : 4
i (minutes): 53
s (seconds): 59
a (am/pm): pm
Be more speciffic about the format would you like your timestamp to have.
I suggest you take a peak at the php date documentation.
Is it using UTC, or something?
I have a PHP version that gives me an error whenever I do something date related without first using date_default_timezone_set. Maybe that'll help you.

Date format conversion in PHP

I have a variable in php:
$d = "19:02:13 Nov 07, 2010 PST";
What is the quickest way to convert the format so I get (mysql DATETIME):
$d = "2010-11-07 19:02:13";
try this
date_default_timezone_set("America/Los_Angeles");
echo date("Y-m-d H:i:s", strtotime("19:02:13 Nov 07, 2010 PST"));
As easy (though not epically efficient) way would be to use strtotime as follows:
date('Y-m-d H:i:s', strtotime('19:02:13 Nov 07, 2010 PST'))
However, make sure you've set the correct local timezone via date_default_timezone_set or this will not work correctly.

Categories