This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 8 years ago.
I am importing data from and excel sheet and the dates are formatted like so:
May 5, 2014
I need to convert this to a proper PHP date format like:
Y-m-d
This isn't working:
$new_date = date('Y-m-d', strtotime('May 5, 2014'));
echo $new_date;
Results
1969-12-31
I was thinking that i could do something like this:
$old_date = 'May 5, 2014';
$old_date = str_replace(',', '', $old_date);
$old_date_parts = explode(' ',$old_date);
// Convert month name to number
$may = '5';
// Build new date
$new_date = $old_date_parts[2].'-'.$may.'-'.$old_date_parts[1];
But there HAS to be another way!
Please help. Thank you in advance.
EDIT
Seems that the PHPExcel that i am using doesn't have the function to convert a serial date to a unix time so i used this.
UNIX_DATE = (EXCEL_DATE - 25569) * 86400
$date = date_create_from_format('M j, Y', 'May 5, 2014');
echo date_format($date, 'Y-m-d');
Or OOP Style
$date = DateTime::createFromFormat('M j, Y', 'May 5, 2014');
echo $date->format('Y-m-d');
Related
In my CodeIgniter application, I am getting date in different formats, such as: April 1st 2017, May 29, 2015, Jun-15-2015, 10-September-2015 and sometimes even with extra string such as Start: April 1, 2017. However, I want to convert the input date from any format to Y-m-d in order to save it in MySQL database. For example, if input date is April 1st 2017 I should get 2017-04-01. I have used below posted code for that but it is not working for all of the above mentioned cases. So please tell how can I write general conversion logic that can convert date from any format even if date has extra string with it (as mentioned above) to Y-m-d format.
Code:
$date = DateTime::createFromFormat('F jS Y', $old_date);
$new_date = $date->format('Y-m-d');
try this
$old_date = 'Jun-15-2015';
echo $newDate = date("Y-m-d", strtotime($old_date));
You can replace the extra string like
$date = str_replace('Start: ',' ',$date);
And after you can use date function of php
echo $date = date('Y-m-d',strtotime($date));
This might help even for inserting this date format into database tables
function convertUTCCombinedToLocal($utcDateTime) {
$utcDateTime = explode(" ", $utcDateTime);
$date = explode("-", $utcDateTime[0]);
$time = explode(":", $utcDateTime[1]);
$localDate = new DateTime();
$localDate->setTimezone(new DateTimeZone('UTC'));
$localDate->setDate($date[0], $date[1], $date[2]);
if (count($time) == 3) {
$localDate->setTime($time[0], $time[1], $time[2]);
} else {
$localDate->setTime($time[0], $time[1], '00');
}
$localDate->setTimezone(new DateTimeZone('Asia/Kolkata'));
return $localDate->format('d/m/Y H:i:s');
}
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
I have this date format: August 22, 2016
I want to convert to 08/22/2016
str_replace is the solution?
eg: str_replace("August","08/",$var);
but it should have at least 12 str_replaces...
any ideas for a simple way?
Refer to DateTime::format
Specify your desired date format 'm/d/Y' after creating the DateTime object.
Try this:
$date = new DateTime('August 22, 2016');
echo $date->format('m/d/Y'); // 08/22/2016
try this,
$originalDate = "August 22, 2016";
$newDate = date("m/d/Y", strtotime($originalDate));
echo $newDate;
OUTPUT
08/22/2016
DEMO
just like php date function convert to in strtotime,
Example:
$date = "August 22, 2016";
$date2 = date("m/d/Y", strtotime($date));
echo $date2;
ANS : 08/22/2016
Note: if you change date format so change in date function so u like
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
I want to convert date format in php.
I have a variable $date = '05-27-2016 15:30'. I want to convert it into '3:30 PM 27-May-2016'.
I try this one date('H:i A d-M-Y',strtotime($date)) but it gives wrong result.
How do I do that?
Use createFromFormat function
$date = DateTime::createFromFormat('m-d-Y H:i', '05-27-2016 15:30');
echo $date->format('h:i A d-M-Y'); // 3:30 PM 27-May-2016
As i Commented out both the format, You need to change the format before changing final for final format.
If you need 3 at 03, just use g at h.
General: Online Check
$date = '27-05-2016 15:30';
echo date('h:i A d-M-Y', strtotime($date)); //03:30 PM 27-May-2016
Or ,
using Object Oriented: Online Check
$date = '05-27-2016 15:30';
$date = DateTime::createFromFormat('m-d-Y H:i', $date);
echo $date->format('h:i A d-M-Y');
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 7 years ago.
How to convert date format using php ?
i tried to convert from 2015-02-24 03:21:56 to Feb 24, 15 03:21:56
i tried this code but not work, How can i do ?
<?PHP
$date = "2015-02-24 03:21:56";
echo $new_date = date('F j, Y, g:i a', $date);
?>
The problem is that you're trying to feed a text string into date(), when really it wants a Unix timestamp. So, you need to convert your input data to a Unix timestamp first, then feed that to date().
Here is the code to do what you want:
<?php
$date = "2015-02-24 03:21:56";
$converted = strtotime($date);
$new_date = date('F j, Y, g:i a', $converted);
echo $new_date;
?>
This question already has answers here:
php convert date to MySQL date
(4 answers)
Closed 9 years ago.
I need to convert Oct 24, 2011 03:16 PM to 2011-10-24 15:16:00 in php.
I know its possible by creating a variable for each month "$jan = 01, $feb = 02, etc" and then using "explode" to split the information by space and re-arranging it in the correct order.
But is there any simpler way to do it?
$string_date = strtotime("Oct 24, 2011");
$date_object = strtotime($string_date);
$formatted = date("Y-m-d h:i:s", $date_object);
Check out the date() function for more formatting options.
The function strtotime() is a very powerful date parser. Use it to get a UNIX timestamp of the incoming date, and use the date() function to output the date in your desired format.
<?php
$ts = strtotime("Oct 24, 2011 03:16 PM");
$date = date("Y-m-d H:i:s", $ts);
?>
echo date("Y-m-d H:i:s", strtotime("Oct 24, 2011 03:16 PM"));
date( 'Y-m-d H:i:s' , strtotime ("Oct 24, 2011 03:16 PM" ) );
The strtotime() option mentioned in other answers here is the easiest method, but don't fall in love with the function for ALL date string->number conversions. It can and WILL screw up at some point. If you know exactly what format a date string will be in, then use the more reliable
$date = DateTime::createFromFormat('M j\, Y', "Oct 24,2011");