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;
?>
Related
This question already has answers here:
How to parse a date string in PHP?
(2 answers)
Closed 1 year ago.
I never thought this will be such a pain. What is the best way to convert back this timestamp to a standard PHP timestamp that strtotime() accepts? :
September 29, 2017, 7:16 UTC
The format is:
date("F j, Y, G:i T")
Build a DateTime object first, then format it to a standard format, this will pass into strtotime.
$date = "September 29, 2017, 7:16 UTC";
$day = new DateTime($date);
$utc = strtotime($day->format("Y-m-d g:i A"));
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
I trying to get a date in the d/m/Y H:i format. Here's my code
$dateStr = "28/07/2016 10:00";
echo date('d/m/Y H:i', strtotime($dateStr));
Here's the ouput:
01/01/1970 01:00
I trying to convert to other formats, but the result still the same date. I know this kind of obvious but still can't understand why i'm getting that date as result.
You can use DateTime class and it's createFromFormat method to parse the string to date in required format.
Like this,
$date = DateTime::createFromFormat('d/m/Y H:i', "28/07/2016 10:00");
$date = $date->format('Y-m-d H:i:s');
echo $date;
Use Following code
$dateStr = "28/07/2016 10:00";
$dateStr =str_replace("/","-",$dateStr);
echo date('d/m/Y H:i', strtotime($dateStr));
This code achieves what you want to do:
$dateString = '28/07/2016 10:00';
$date = \DateTime::createFromFormat('d/m/Y H:i', $dateString);
echo($date->format('d/m/Y H:i'));
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 6 years ago.
I have code:
$date= "2016/Apr/16";
$time="5:00 PM";
I want to convert it to DB format like 2016-04-16 17:00:00 in php .
I am not getting exact code from anywhere.
This way will solve your problem.
<?php
$date= "2016/Apr/16";
$time="5:00 PM";
$fullDate = str_replace('/','-',$date) . " " . $time;
$newDate = date("Y-m-d H:i:s", strtotime($fullDate));
echo $newDate;
?>
Result
More Examples;
http://www.w3schools.com/php/func_date_strtotime.asp
Convert date format yyyy-mm-dd => dd-mm-yyyy
Normally you would use the following code:
date('Y-m-d H:i:s', strtotime($date.' '.$time));
If it returns the year 1970, then the input date (the one from $date variable) is not formatted according to a standard that can be recognised by php.
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 8 years ago.
I get the date in the variable date as
$date = "<?php echo $projects->dob;?>" ; //where let dob=2013-01-12
I want to convert this into Jan 2013 format. How to convert in php?
do:
echo date('M Y', strtotime($your_date));
see: Date Formats
$dob = '2013-01-12';
echo date('M Y', strtotime($dob));
$date = date('M Y', strtotime($projects->dob));
Example:
https://ideone.com/tdJl8O
$timestamp = strtotime("2013-01-12");
$formatted = date("M Y", $timestamp);
Demo: http://codepad.org/Lw2eEq6Q
echo $date = date("M Y",strtotime($projects->dob)) ;