PHP Date Format Converting

Convert a date format in PHP

Use strtotime() and date():

$originalDate = "2010-03-21";
$newDate = date("d-m-Y", strtotime($originalDate));

(See the strtotime and date documentation on the PHP site.)

Note that this was a quick solution to the original question. For more extensive conversions, you should really be using the DateTime class to parse and format :-)

PHP convert date format dd/mm/yyyy = yyyy-mm-dd

Dates in the m/d/y or d-m-y formats are disambiguated by looking
at the separator between the various components: if the separator is a
slash (/), then the American m/d/y is assumed; whereas if the
separator is a dash (-) or a dot (.), then the European d-m-y
format is assumed. Check more here.

Use the default date function.

$var = "20/04/2012";
echo date("Y-m-d", strtotime($var) );

EDIT I just tested it, and somehow, PHP doesn't work well with dd/mm/yyyy format. Here's another solution.

$var = '20/04/2012';
$date = str_replace('/', '-', $var);
echo date('Y-m-d', strtotime($date));

Converting Date format in PHP

Use strtotime() and date():

$originalDate = "Mon Apr 22 2013 12:16:00 GMT+0530 (India Standard Time)" ;
$newDate = date("Y-m-d H:i:s", strtotime($originalDate));

(see strtotime and date docs on the PHP site).

or use DateTime:

<?php
$source = "Mon Apr 22 2013 12:16:00 GMT+0530 (India Standard Time)";
$date = new DateTime($source);
echo $date->format("Y-m-d H:i:s"); // 22 2013 12:16:00
echo $date->format("Y-m-d H:i:s"); // 22 2013 12:16:00
?>

DEMO

Convert one date format into another in PHP

The second parameter to date() needs to be a proper timestamp (seconds since January 1, 1970). You are passing a string, which date() can't recognize.

You can use strtotime() to convert a date string into a timestamp. However, even strtotime() doesn't recognize the y-m-d-h-i-s format.

PHP 5.3 and up

Use DateTime::createFromFormat. It allows you to specify an exact mask - using the date() syntax - to parse incoming string dates with.

PHP 5.2 and lower

You will have to parse the elements (year, month, day, hour, minute, second) manually using substr() and hand the results to mktime() that will build you a timestamp.

But that's a lot of work! I recommend using a different format that strftime() can understand. strftime() understands any date input short of the next time joe will slip on the ice. for example, this works:

$old_date = date('l, F d y h:i:s');              // returns Saturday, January 30 10 02:06:34
$old_date_timestamp = strtotime($old_date);
$new_date = date('Y-m-d H:i:s', $old_date_timestamp);

How to change date format d/m/Y to Y-m-d PHP

Use DateTime objects when you're working with dates and times. You can use DateTime::createFromFormat() to parse the date string and then the DateTime::format() to format it the way you want:

$str = '24/12/2013';
$date = DateTime::createFromFormat('d/m/Y', $str);
echo $date->format('Y-m-d'); // => 2013-12-24

For a list of available formatting options, see the documentation.

How can I convert the date format from html5 php

What you're doing is feeding the datetime object with a string literal, not the value of the variable from your $_POST:

$newDate = new DateTime('$date');

I don't know if thats a typo, but you should change it to this: Example:

$date = $_POST['date'];
$newDate = new DateTime($date); // remove those quotations
$newDate->modify('+30 day');
// should not be YY-mm-dd but Y-m-d
echo $newDate->format('Y-m-d');// 2014-07-20
// not echo $date->format('YY-mm-dd'); $date is not your datetime object

how to convert date format to datetime format using php

Why don't you use the DateTime object in PHP (http://php.net/manual/en/class.datetime.php)?

Here is how i would do it:

$date = DateTime::createFromFormat('d/m/Y', $travelDate);
echo $date->format('Y-m-d H:i:s');

Hope it helps.

php date format conversion from d/m/Y to date('D, j M Y H:i:s')

try with this and see if it works:

for($i=0; $i<count($playing_dates);$i++){
$date = DateTime::createFromFormat('d/m/Y', $playing_dates[$i]);
$newformat = $date->format('D, j M Y H:i:s');
$playing = strtotime($date);

echo $playing_dates[$i]." --> ".$playing." -->";
echo $newformat ."</br>";
}

EDIT:

<?php
$playing_dates='06/03/2013|13/03/2013|20/03/2013|27/03/2013|03/04/2013|10/04/2013|17/04/2013|24/04/2013|01/05/2013|08/05/2013|15/05/2013|22/05/2013|29/05/2013';
$playing_dates = explode("|", $playing_dates);
for($i=0; $i<count($playing_dates);$i++){
$date = DateTime::createFromFormat('d/m/Y', $playing_dates[$i]);
$newformat = $date->format('D, j M Y H:i:s');
$playing = strtotime($date->format("Y-m-d"));

echo $playing_dates[$i]." --> ".$playing." -->";
echo $newformat ."</br>";
}
?>

06/03/2013 --> 1362524400 -->Wed, 6 Mar 2013 18:31:10
13/03/2013 --> 1363129200 -->Wed, 13 Mar 2013 18:31:10
20/03/2013 --> 1363734000 -->Wed, 20 Mar 2013 18:31:10
27/03/2013 --> 1364338800 -->Wed, 27 Mar 2013 18:31:10
03/04/2013 --> 1364940000 -->Wed, 3 Apr 2013 18:31:10
10/04/2013 --> 1365544800 -->Wed, 10 Apr 2013 18:31:10
17/04/2013 --> 1366149600 -->Wed, 17 Apr 2013 18:31:10
24/04/2013 --> 1366754400 -->Wed, 24 Apr 2013 18:31:10
01/05/2013 --> 1367359200 -->Wed, 1 May 2013 18:31:10
08/05/2013 --> 1367964000 -->Wed, 8 May 2013 18:31:10
15/05/2013 --> 1368568800 -->Wed, 15 May 2013 18:31:10
22/05/2013 --> 1369173600 -->Wed, 22 May 2013 18:31:10
29/05/2013 --> 1369778400 -->Wed, 29 May 2013 18:31:10


Related Topics



Leave a reply



Submit