Date convert showing wrongly [duplicate] - php

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 5 years ago.
I like to convert a date format dd/mm/yyyy to another format in PHP.
My Code:
$date = '29/01/2018';
echo date('l jS F Y', strtotime($date));
When I run the above code, it's showing me some wrong date:
Thursday 1st January 1970
Am I doing anything wrong?

You could also use the DateTime objects to help you converting dates. You can "create a datetime object from an specified format" and convert it.
$date = "29/01/2018";
$dt = DateTime::createFromFormat("d/m/Y", $date);
echo $dt->format("l jS F Y");

signifies American M/D/Y formatting
<?php
$date = '01/29/2018';
echo date('l jS F Y', strtotime($date));
output
Monday 29th January 2018

Related

convert date format in php like 6th of November 2017 to 2017-11-6 [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 5 years ago.
i want to convert a date format to another using php
6th of November 2017 to 2017-11-6
i tried
$newDate = date("Y-m-d", strtotime('6th of November 2017'));
but it always returns 01-01-1970
When you create date from specific format then use createFromFormat of date.
$date = DateTime::createFromFormat('jS \o\f F Y', '6th of November 2017');
echo $date->format('Y-m-d');
Note: If you have string in your format then you have to escape that string (with \) for conversation
DEMO
Replace 6th of to 6th and it will be fine..
$newDate = date("Y-m-d", strtotime('6th November 2017'));
echo ($newDate);
result
2017-11-06
Try this:
$newDate = date("Y-m-d", strtotime("6 November 2017"));

PHP - Convert this string to another date/time format [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 5 years ago.
I'm working with an API that returns timestamps in this format:
07/23/2017 23:39:21
The format is: MM/DD/YYYY HH:MM:SS
I need to covert this string so it displays in this format on my webpage:
23 July 2017, 11:39pm
Is this possible?
You can do this :
$date = new DateTime('07/23/2017 23:39:21');
echo $date->format('d F o, h:ia');
Output :
23 July 2017, 11:39pm
Take a look at http://php.net/manual/de/datetime.createfromformat.php. You'll get a DateTime object which you can convert in the format you desire.
Like so:
<?php
$date = DateTime::createFromFormat('m/d/Y H:i:s', '07/23/2017 23:39:21');
echo $date->format('d M Y, h:ia');
?>

convert date August 22, 2016 to 08/22/2016? [duplicate]

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

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.

Categories