I need to get data from RSS feed and then save it to MySQL. The problem is that in RSS feed datetime format is like this: Sun, 09 Nov 2014 12:00:38 +0200 How I could convert it to format so I could save it to database? and how to convert it back later when I want to display it again with the same format?
Try this
$DateTime= date("Y-m-d H:i:s", strtotime("Sun, 09 Nov 2014 12:00:38 +0200"));
echo $DateTime;
To retrieve back from db, in your select query use
DATE_FORMAT(date_column, '%a %d %b %Y %T')
if you are using PHP along with MySQL, strtotime() is nice php function :)
http://php.net/manual/en/function.strtotime.php
date_default_timezone_set('UTC');
$date_string = 'Sun, 09 Nov 2014 12:00:38 +0200';
echo 'original string: '.$date_string.'<br/>';
$unix_time_stamp = strtotime($date_string );
echo 'timestamp: '.$unix_time_stamp.'<br/>';
$old_format = date("D, j M Y H:i:s O", $unix_time_stamp );
echo 'back to originalt: '.$old_format;
Example on http://viper-7.com/KI5LfG
You can get any date format you desire with php date()
http://php.net/manual/en/function.date.php
Related
I have a field within a MySQL database that has a date format of the following:
Mon, 17 Aug 2015 19:14:22 +0100
I want to display this date using PHP in the following format:
YYYY-MM-DD HH:MM:SS
I have no idea how to pick out the different elements and reformat them.
Can somebody help?
Many thanks,
John
You can try these :-
$date = "Mon, 17 Aug 2015 19:14:22 +0100";
$newDate = date('Y-m-d H:i:s', strtotime($date));
echo $newDate;
You can do it in mysql
SELECT
DATE_FORMAT(test.dateFrom, '%Y-%M-%d %H:%i:%s') as date,
FROM test
Or in php
$date = date('Y-m-d H:i:s', strtotime($datefrommysql) );
I have a php script that returns the date of the server:
<?php
echo date('D, d M y H:i:s a');
?>
but when I print this value on the client site I get:
Tue, 29 Sep 15 16:19:28 pm
But instead I need the date in this format:
Tue Sep 29 2015 16:18:00 GMT+0200 (Central Europe Daylight Time)
How should I modify my php script then to have it like this?
Thanks!
echo (new DateTime())->format('r');
$datestring = "26-08-2015 03:35:28"; //date as string
$date = new DateTime($datestring); //String to datetime conversion
$date = $date->format('D d M y H:i:s O e'); //format the date
echo $date;
This is the manual link. Timezone you have to set.
Output will look like Wed 26 Aug 15 03:35:28 +0200 Europe/Paris
Go to the very bottom of this page http://php.net/manual/en/function.date-default-timezone-set.php
I try to insert a rss pubdate to my MySQL database. The format of the date is:
Tue, 16 apr 2013 17:04:08 UT
I try different function to format this pubdate, but I have problems with the timezone UT in the date. Functions I tried:
strftime("%a, %d %b %G %H %M %S", strtotime($pubdate));
date_create_from_format('D, d M Y G:i:s U', $pubdate);
Can anybody help me?
You can do like this for inserting pubdate from RSS feed into mysql DateTime field
<?php
function pubDateToMySql($str) {
return date('Y-m-d H:i:s', strtotime($str));
}
$pubDate = 'Wed, 29 Jul 2015 06:59:41 +0000';
$mysql_datetime = pubDateToMySql($pubDate);
echo $mysql_datetime ;
Output
2015-07-28 23:59:41
ref : http://www.timwickstrom.com/server-side-code/php/convert-rss-pubdate-to-mysql-datetime/
you are trying to insert in to a mySQL DateTime field right?
so your goal is to get it to be in this format (YYYY-MM-DD HH:MM:SS)
you can try this using strptime:
$rssdate = "Tue, 16 apr 2013 17:04:08 UT";
$parsed = strptime($rssdate,'%a, %d %b %Y %H:%M:%S %Z');
$sqldate = date("Y-m-d H:i:s", mktime($parsed['tm_hour'], $parsed['tm_min'], $parsed['tm_sec'], $parsed['tm_mon']+1, $parsed['tm_mday'], 1900+$parsed['tm_year']));
after that you should have gotten $sqldate with a value of
2013-04-16 17:04:08
take note that the UT will be discarded because of the DateTime field not being able to handle it. If you really need it, then just change your row into a String type if you can.
Another thing, i tried using the date_create_from_format too but it seems that it cant recognize the month of April as 'apr' since in the format it needs to be capitalized like 'Apr'.
Does anyone could help me how to convert data from email header?
I have the next date format from email header:
Wed, 28 Apr 2010 21:59:49 -0400
I need to convert them into mysql Date, or timestamp. Thanks!
You should be using DateTime for this, specifically DateTime::createFromFormat():
$str = 'Wed, 28 Apr 2010 21:59:49 -0400';
$date = DateTime::createFromFormat( 'D, d M Y H:i:s O', $str);
Now, you have a Date object in $date, and you can grab the unix timestamp (if that's what you want), or you can format it into a date for MySQL.
echo $date->getTimestamp(); // Outputs: 1272506389
echo $date->format( 'Y-m-d H:i:s'); // For MySQL column, 2010-04-28 21:59:49
You can see it working in the demo.
How to convert "00:00:00.000 GMT Mon Nov 22 2010" to more user friendly format.
You could use these PHP functions:
http://php.net/manual/en/function.strtotime.php
http://php.net/manual/en/function.date.php
strtotime will convert your current to a unix timestamp.
You can then format that unix time stamp however you want, using date().
Why not with strtotime?!
echo date('d/m/Y',strtotime('0:00:00.000 GMT Mon Nov 22 2010')); // I'm french
You might also find these useful:
$mysqldate = date( 'Y-m-d H:i:s', $phpdate );
$phpdate = strtotime( $mysqldate );
And in CakePHP the TimeHelper http://book.cakephp.org/view/1470/Time:
echo $time->nice($mysqlDate);
etc.
Try this:
//initial format
echo '0:00:00.000 GMT Mon Nov 22 2010';
//unix format: 2010-11-22
echo date('Y-m-d',strtotime('0:00:00.000 GMT Mon Nov 22 2010'));
//explicit format: Monday 22 November 2010
echo date('l d F Y',strtotime('0:00:00.000 GMT Mon Nov 22 2010'));
First convert the string to a datetime using strtotime:
strtotime
and then format the datetime anyway you want using sprintf:
sprintf
Use strtotime function:
echo strtotime("00:00:00.000 GMT Mon Nov 22 2010"), "\n";
check on php manual and try strtotime and date function