Display reformatted date from MySQL in PHP - 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) );

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

how can I format my date in php to have the query new Date() format?

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

Transform date to valid format for PostgreSQL with 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

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

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