I have a string in the format Apr 27 2014 and I need to convert to a date in the format yyyy-mm-dd. Is there a easy straight forward way to accomplish that? I couldn't find anything besides write a little parser.
This should work for you:
$date = new DateTime("Apr 27 2014");
echo $date->format('Y-m-d');
Output:
2014-04-27
For more information about the DateTime class see the manual: http://php.net/manual/en/class.datetime.php
You can also use strtotime() and date() functions:
<?php
echo date("Y-m-d", strtotime('Apr 27 2014'))."\n";
You can look at other examples with strtotime() at php.net.
Notice: unix timestamp is limited on 32bit systems form ~1900 till 2038.
Related
I have a date in this format "Wed, 26 Oct 2022 16:11:30 -1100", need to convert it to UTC time and in a format so it can be inserted into a MySQL database.
The date was pulled from an email using "$headerInfo->date;". I don't see any way to receive the date any different.
Every way I try to do the conversion is painful and brute force.
Is there an elegant, or not painful way to do this?
TIA.
Been trying regex but it doesn't handle converting the month into digits, then you have the UTC offset (time zone) to work with.
Parse the date/time string using DateTimeImmutable::createFromFormat, set the timezone to UTC then format it to MySQL's datetime literal syntax
$dt = DateTimeImmutable::createFromFormat('D, j M Y H:i:s O', $dateString);
$mysqlFormat = $dt->setTimezone(new DateTimeZone('UTC'))->format('Y-m-d H:i:sP');
Demo ~ https://3v4l.org/G7RBG
<?php
//just use strtotime
echo date('Y-m-d H:i:s e',strtotime('Wed, 26 Oct 2022 16:11:30 -1100'));
?>
I am receiving JSON data with a date string with this format:
'Mon Jun 30, 2014'
What would be the way to convert this to a datetime? Checking the PHP functions for this I got unsecure if better to use date_parse_from_format or date_create_from_format.
What are the differences and which would suit better for this task?
DateTime::createFromFormat would work well here. It allows you to format the string easily and also takes timezones into consideration when appropriate and can be easily used in comparisons without having to convert to a timestamp first.
$date = DateTime::createFromFormat('D M d, Y', 'Mon Jun 30, 2014');
echo $date->format('Y-m-d');
You can convert to a unix timestamp using strtotime(). I don't know what you mean by a "datetime", but if you mean something like for MySQL then you format the timestamp with date() (you can include time but it isn't present in the original string):
echo date('Y-m-d', strtotime($string));
The second of the two likely fits you better ---
The first one only breaks down the date into an array, so you can work with the parts, individually.
But the second returns the DateTime object you are looking for.
I'm a computer science student and I have been stumped on part of my assignment. We have to parse an XML file and save it in a mySQL database. The problem I have run into is that there is an element in the XML file called pubDate that has a string that is formatted like this:
Mon, 04 Nov 2013 11:08:58 -0600.
I have looked on multiple websites and have not been able to find any way to convert that string to dateTime.
Any help would really be appreciated.
Thanks
You can use like this
Example #1 DateTime::setTimeZone() example
Object oriented style
$date = new DateTime( '2008-02-07 16:45:58', new DateTimeZone('Pacific/Nauru'));
echo $date->format('D, d M Y G:i:sP') . "\n";
Output is like this
Thu, 07 Feb 2008 16:45:58+12:00
Please see this link also http://us1.php.net/datetime.settimezone.php
you can try strtotime() function to convert to unix time stamp and then strftime() to convert to mysql date format
try strtotime() function for php, if your date time is store in varchar, char or text in mysql use mysql STR_TO_DATE() function.
Take a look at the functionality provided with PHP's new Date/Time class. As long as you have the originating format of the string, you can easily convert it to an object and then back to whatever string format you need.
$d = DateTime::createFromFormat("D, d M Y h:i:s P", "Mon, 04 Nov 2013 11:08:58 -0600");
Then export it as you see fit.
I'm trying to convert a time format in the format below to a unix timestamp using PHP
j n Y H:i:s
Im trying to find a way to convert to a unix timestamp so it can be used in SQL databases. An example of the dates that I need to convert:
28 Mar 12 16:37:34
I've tried functions called "strptime" and "mktime" that I found on stackoverflow to no success - im not really sure what Im doing with them. If this is the answer here, could someone explain how to use them? Ive tried to understand the PHP documentation but Im just not getting it.
The post I was reading is here: PHP date format converting
echo strtotime('28 Mar 12 16:37:34'); //1332945454
http://php.net/manual/en/function.strtotime.php
If you need ultimate flexibility on parsing the format, use DateTime::createFromFormat()
$dt = DateTime::createFromFormat(
'j M y H:i:s', $dateString, new DateTimeZone('Your/Timezone'));
$timestamp = $dt->getTimestamp();
The php way is to use date() and strtotime()
sql uses YYYY-MM-DD HH:MM:SS
$dateTime = date('Y-m-d H:i:s', strtotime('28 Mar 12 16:37:34'));
I've a date like Tue Dec 15 2009. How can I convert it into seconds?
Update:
How can I convert a date formatted as above to Unix timestamp?
I assume by seconds you mean a UNIX timestamp.
strtotime() should help.
You can use the strtotime function to convert that date to a timestamp :
$str = 'Tue Dec 15 2009';
$timestamp = strtotime($str);
And, just to be sure, let's convert it back to a date as a string :
var_dump(date('Y-m-d', $timestamp));
Which gives us :
string '2009-12-15' (length=10)
(Which proves strtotime did understand our date ^^ )
[edit 2012-05-19] as some other questions might point some readers here: Note that strtotime() is not the only solution, and that you should be able to work with the DateTime class, which provides some interesting features -- especially if you are using PHP >= 5.3
In this case, you could use something like the following portion of code :
$str = 'Tue Dec 15 2009';
$format = 'D F d Y';
$dt = DateTime::createFromFormat($format, $str);
$timestamp = $dt->format('U');
DateTime::createFromFormat() allows one to create a DateTime object from almost any date, no matter how it's formated, as you can specify the format you date's in (This method is available with PHP >= 5.3).
And DateTime::format() will allow you to format that object to almost any kind of date format -- including an UNIX Timestamp, as requested here.
You mean like an UNIX-timestamp? Try:
echo strtotime('Tue Dec 15 2009');