D d/m/y Convert to another format PHP [duplicate] - php

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Date in a URL dd/mm/yyyy
(1 answer)
Closed 4 years ago.
I have date stamps from external source as per below format
Tue 6/02/2018
However when trying to convert the date to Y-m-d for SQL format the date get skewed, using the below;
date("Y-m-d H:i:s", strtotime('Tue 6/02/2018');
//ends up being Y-d-m
I have set the timezone earlier using
date_default_timezone_set('Australia/Brisbane');

strtotime() cannot parse the format "Tue 6/02/2018" correctly. You have to use DateTime::createFromFormat() to parse from your format.
$time = 'Tue 6/02/2018';
$date = DateTime::createFromFormat("D j/m/Y", $time) ;
echo $date->format("Y-m-d H:i:s") ; // 2018-02-06 07:12:19
You also could use new DateTimeZone() as:
$time = 'Tue 6/02/2018';
$tz = new DateTimeZone('Australia/Brisbane');
$date = DateTime::createFromFormat("D j/m/Y", $time, $tz) ;
echo $date->format("Y-m-d H:i:s") ; // 2018-02-06 17:17:48

Related

Convert date format from Y-m-d H:i:s into dmy [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 4 years ago.
I have a date time in string with format Y-m-d H:i:s like this:
$dateTime = '2018-07-06 18:53:21';
i want to conver it into dmy format, like this:
$convertedDateTime = $this->convertDateTime($dateTime);
echo $convertedDateTime;
and the result i expected from above echo is 060718, how can i achieve this?
You can use date_format like this:
echo date_format($date,"Y/m/d H:i:s"); //e.g. 2013/03/15 00:00:00
echo date_format($date,"dmy"); //e.g. 150313 -- Y capital would return 2013
there's two methods to use:
date - https://secure.php.net/manual/en/function.date.php
usage example:
$myDate = '2018-07-06 09:49:00';
$myDate = date('d-m-y', strtotime($myDate));
but more often than not you'll want to use DateTime https://secure.php.net/manual/en/class.datetime.php:
$myDate = DateTime::createFromFormat('Y-m-d H:i:s', '2018-07-06 09:49:00');
$$newDate = $myDate->format('d-m-Y');
\DateTime::createFromFormat('Y-m-d H:i:s', '2018-07-06 18:53:21')->format('dmy');

How to convert date('H:i A d-M-Y') format in php [duplicate]

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

convert Y/M/d H:i to db format in php [duplicate]

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.

Convert date and time format in passed date value from db [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
I have a date value from the database and its format is: YYYY-mm-dd hh:mm:ss
I want to convert it to this format: YYYY/mm/dd hh:mm
Here is my code:
<?php
$datetime = $value['last_online']; //from db
$date = date('d-m-Y', $datetime);
$time = date('Gi.s', $datetime);
echo $date.' '.$time;
?>
But it's not working and is giving me an error of:
A non well formed numeric value encountered
I want to get rid of the minus signs and the seconds so my new date and time format would be YYYY/mm/dd hh:mm. How do I do it?
Any ideas?
Use like below
$datetime = $value['last_online']; //from db
$date = date('Y/m/d h:i', strtotime($datetime));

function to convert EDT to system time [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 7 years ago.
I have a function to convert EDT to system time,
function edttogmt($date){
$date = str_replace('$','', $date);
$date = str_replace('EDT','', $date);
$date = date('Y-m-d H:i:s',strtotime($date));
return $date;
}
And my date is like $date = '$07/23/2015 12:38:23 PM EDT';
Is it possible to convert this to my server time.The above function creates wrong
You can exactly describe the input format to receive DateTime object
$date = DateTime::createFromFormat('$m/d/Y h:i:s A e', '$07/23/2015 12:38:23 PM EDT');
echo $date->format('Y-m-d H:i:s'); // 2015-07-23 12:38:23

Categories