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