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

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

Related

Date convert showing wrongly [duplicate]

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

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

Converting a date with dot separators into another format without dots [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 5 years ago.
From a JSON payload I get a date formatted like so: "2017.03.11". The desired result is: "11 MARCH 2017". I know how to remove the dots and convert the result into what I want using date(). But is there a more direct way to achieve it (without the step removing the dots)?
Try this:
$date = DateTime::createFromFormat('Y.m.d', '2017.03.11');
and then with the $date object you can do whatever you need.
This should do what you need.
<?php
$date = "2017.03.11";
$date1 = str_replace(".", "-", $date);
$date1 = strtotime($date1);
$date2 = date('d F Y',$date1);
echo $date2;
?>
2017.03.11 input
11 March 2017 output

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