How to Convert Date String into Different format? [duplicate] - php

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 5 years ago.
I have a date that gets sent from a database which comes in a string format of '2018-01-01'. What I want to do is convert it into a string of month and year. E.G. 'January 2018'.
What would be the best way of doing so using PHP?

Try using the strtotime function to turn your string into a timestamp and the date function with the F Y format to get what you want:
$date = "2018-01-01";
$formatted = date("F Y", strtotime($date));

Related

How would I change one date format to another in PHP [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 4 years ago.
How would I change the date 18/08/2018 10:46:15 pm to Sat 25, 10:46:15 pm in PHP. I am doing the following:
```
$OldDate = "18/08/2018 10:46:15 pm";
$NewDate = $OldDate->format(D-j, Y-H:I:S);
echo $NewDate;
?>```
However, it is not showing anything, am I formatting it correctly?
Your old date is just a string. If you want to format it, you need to create a DateTime object using that string the constructor.
You're also incorrectly specifying the format
$OldDate = new DateTime("18/08/2018 10:46:15 pm");
echo $OldDate->format("L-j, h:i:s A");
http://php.net/manual/en/class.datetime.php

String to Date or Date and Time [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 4 years ago.
I have a string like this "20180720171534449" which is a kind of time stamp, is there an easy way I can convert this using PHP and format it as a date or date and time that makes sense to a human?
TIA
Peter
You have an 'YmdHisv' format where v is miliseconds.
Miliseconds is not parsable (as I found out today) with date_create_from_format so you need to remove that first from the string with substr.
$s = "20180720171534449";
$date = date_create_from_format('YmdHis', substr($s,0,-3));
echo date_format($date, 'Y-m-d H:i:s'); //2018-07-20 17:15:34
https://3v4l.org/m1XNd
As Ghost pointed out milliseconds is parasble if using microseconds u instead.
$s = "20180720171534449";
$date = date_create_from_format('YmdHisu', $s);
echo date_format($date, 'Y-m-d H:i:s\.v'); //2018-07-20 17:15:34.449

How to change date format dmYhis to Y-m-d in PHP [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Convert this string to timestamp PHP [duplicate]
(2 answers)
Convert a date format in PHP [duplicate]
(18 answers)
Closed 5 years ago.
How to change date format dmYhis to Y-m-d in PHP. I have using the date() function but I cannot seem to make it work
$d = '24032017191551';
echo date('Y-m-d', strtotime($d));
Output: 1970-01-01
i want to get the ans is 2017-03-24
Below is the code to change date format
$retrieved ='24032017191551';
$date = DateTime::createFromFormat('dmYHis', $retrieved);
echo $date->format('Y-m-d');

Need to change my date format [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
How can I change the format of my date from 2016-10-05 to 10/05/2016 in php
$date= $row['date'];
So when the row echos out it'll all be the new format
Use DateTime to create an object (from the current format), allowing you to reformat the output.
$date = "2016-10-05";
$date = DateTime::createFromFormat('Y-d-m', $date);
echo $date->format('d/m/Y');
https://repl.it/DqvO

Month and Year date conversion [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
I have a Credit card expiry date with month and year only coming in form of a string say 08/17, How can I change this string in a format so that I can pass it to Authorize.net
$creditCard->setExpirationDate( "2017-08");
I have tried to use strtotime() but it is giving me the current year
echo $date = date('Y-m', strtotime($ccInfo['exp_date']));
You should use date_create_from_format instead of strtotime to build your date:
echo date_create_from_format('m/y', '08/17')->format('Y-m');
The function creates a \DateTime object, so you can call format to get the format you want.
You can also write like this
$eventDate = DateTime::createFromFormat('m/y', '08/17');
echo date_format($eventDate, 'Y-m');

Categories