Insert rss pubdate to MySQL database - 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'.

Related

convert date in php day/month name, year

I have a query that inserts date in this format
$time = date("m-d-y");
But when I Fetch and output date it shows wrong date. real: October 2020 but outputs: January 1970
$time = $row1['time'];
$newDate = date('F Y', strtotime($time));
echo $newDate;
How do I output date in this format: 20 OCT, 2020
Try not to store date/time using varchar datatype. If you can change it, please do. However, if you can't change database structure, you can use date_create_from_format() to create a DateTime object from a custom format:
echo date_create_from_format('m-d-y', "10-29-20")->format('F Y');
Output:
October 2020
Edit: Change the format part to ->format('d M, Y') to match your desired format
Output:
29 Oct, 2020

Display reformatted date from MySQL in PHP

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

PHP Date time conversion to save to MySQL

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

Re-Arrange the format of a date stored in a Variable

I would like to re-structure the format of a date that is stored in a variable.
I retrieve the date/time from an MySQL database stored as YYYY-MM-DD HH:MM:SS and I am using PHP.
I would like to display the date as
Thu 2nd Mar 2013 2:30pm
Is this possible?
Thanks.
Please look into these date formats.
http://php.net/manual/en/datetime.formats.date.php
And using this php code (I've not putted down the right format yet!)
$datetime = strtotime($mysqldatevalue);
$mysqldate = date("Y-m-d H:i:s", $datetime);
Yes, it's possible. Use strtotime() to change the SQL date into a unix timestamp, then feed that as the second parameter to date() along with the appropriate format string.
date("d/m/Y H:i:s",strtotime($time_from_db)) will convert 2012-01-01 10:12:14 to 01/01/2012 10:12:14, you can replace the first parameter the fit what you need from the specifications in the date function in php site
$date = date("Y-m-d H:i:s", strtotime($dbdate));
You can also to this directly in your query:
DATE_FORMAT(datecolumn,'%Y-%m-%d %H:%i:%s') as date
Even you can use this: since you mentioned:
I would like to display the date as
Thu 2nd Mar 2013 2:30pm
Is this possible?
Query: syntax: '%a %D %b %Y %r'
SELECT DATE_FORMAT('2013-03-02 14:30:00',
'%A %D %b %Y %r');
| DATE_FORMAT('2013-03-02 14:30:00',
'%A %D %B %Y %R') |
-----------------------------------------------------------------------
| Sat 2nd Mar 2013 02:30:00 PM |

Convert data from email header

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.

Categories