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

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

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

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

PHP Convert date using strtotime? [duplicate]

This question already has answers here:
php strtotime not working
(3 answers)
Closed 7 years ago.
below is my php code.
<?php
$date = "06 06 2015 6:05:00 am";
$cdate = date("Y-m-d H:s:i",strtotime($date));
echo $cdate;
?>
Output is
1970-01-01 01:00:00
i need to 2015-06-06 6:05:00
Thanks
Try this
<?php
$date = "06 06 2015 6:05:00 am";
$datetime= \DateTime::createFromFormat("m d Y h:i:s a", $date);
$cdate = $datetime->format('Y-m-d H:s:i');
You want DateTime::createFromFormat() (http://php.net/manual/en/datetime.createfromformat.php). Using that you can convert a string to a datetime any way you want to.
If this is all you plan to do with dates, continue, otherwise, consider using a good date and time class like Carbon to do more with dates and generally save yourself a lot of effort.
$date = "06 06 2015 6:05:00 am";
$cdate = date_create_from_format('m d Y g:i:s a', $date);
This allows you to specify your own format using format strings. For the entire list of format strings see: http://php.net/manual/en/datetime.createfromformat.php
The resulting $cdate is a DateTime object and implements the DateTimeInterface (http://php.net/manual/en/class.datetimeinterface.php) so you can use the format() method to convert the output to any format you need.
For more on format(): http://php.net/manual/en/datetime.format.php
$date = "06 06 2015 6:05:00 am";
this format is not correct because first date or month but it will first year with the match of strtotime "Y-m-d H:s:i" and "06 06 2015" this is with space but it will be "-" sign.
$date = "2015-06-06 6:05:00 am";
$cdate = date("Y-m-d H:s:i",strtotime($date));
echo $cdate;

PHP: Simple way to convert "Oct 24, 2011" date to "2011-10-24"? [duplicate]

This question already has answers here:
php convert date to MySQL date
(4 answers)
Closed 9 years ago.
I need to convert Oct 24, 2011 03:16 PM to 2011-10-24 15:16:00 in php.
I know its possible by creating a variable for each month "$jan = 01, $feb = 02, etc" and then using "explode" to split the information by space and re-arranging it in the correct order.
But is there any simpler way to do it?
$string_date = strtotime("Oct 24, 2011");
$date_object = strtotime($string_date);
$formatted = date("Y-m-d h:i:s", $date_object);
Check out the date() function for more formatting options.
The function strtotime() is a very powerful date parser. Use it to get a UNIX timestamp of the incoming date, and use the date() function to output the date in your desired format.
<?php
$ts = strtotime("Oct 24, 2011 03:16 PM");
$date = date("Y-m-d H:i:s", $ts);
?>
echo date("Y-m-d H:i:s", strtotime("Oct 24, 2011 03:16 PM"));
date( 'Y-m-d H:i:s' , strtotime ("Oct 24, 2011 03:16 PM" ) );
The strtotime() option mentioned in other answers here is the easiest method, but don't fall in love with the function for ALL date string->number conversions. It can and WILL screw up at some point. If you know exactly what format a date string will be in, then use the more reliable
$date = DateTime::createFromFormat('M j\, Y', "Oct 24,2011");

Categories