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.
Related
i am trying to display a stored DateTime with this format [2016-10-05 11:58:04]. What i want to do is, display the stored date into this readable format [Wed, Oct 10, 2016].
Use date() function. First param is format and 2nd param is timestamp:
$time = "2016-10-05 11:58:04";
echo date("D, M d, Y", strtotime($time)); //output: Wed, Oct 05, 2016
If you want to use datetime object then:
$time = "2016-10-05 11:58:04";
$date = new DateTime($time);
echo $date->format('D, M d, Y'); //output: Wed, Oct 05, 2016
you can use the method format to choose what to display :
<?php
$d = DateTime::createFromFormat("Y-m-d H:i:s", "2016-10-05 11:58:04");
var_dump($d->format("c"));
look the help here : http://php.net/datetime.format
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 have a string in this format:
Thu, 26 Feb 2015 11:39:59
And I wonder if its possible to transform it to a valid timestamp format in order to insert it in my PostgreSQL database.
So ideally in the end I would have something like:
2015-03-26 11:39:59
Is there a function in PHP to do something like this?
Use DateTime() to format date string.
$date = 'Thu, 26 Feb 2015 11:39:59';
$date = new DateTime($date);
echo $date->format('Y-m-d H:i:s');
You can also use it as DateTime::createFromFormat
$date = 'Thu, 26 Feb 2015 11:39:59';
$date = DateTime::createFromFormat('D, d M Y H:i:s', $date);
echo $date->format('Y-m-d H:i:s'); //2015-03-26 11:39:59
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