How to Re-Format Datetime String in PHP

how to re-format datetime string in php?

why not use date() just like below,try this

$t = strtotime('20130409163705');
echo date('d/m/y H:i:s',$t);

and will be output

09/04/13 16:37:05

How to reformat date in PHP?

date("F d, Y", strtotime($input))

Converting string to Date and DateTime

Use strtotime() on your first date then date('Y-m-d') to convert it back:

$time = strtotime('10/16/2003');

$newformat = date('Y-m-d',$time);

echo $newformat;
// 2003-10-16

Make note that there is a difference between using forward slash / and hyphen - in the strtotime() function. To quote from php.net:

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.

To avoid potential ambiguity, it's best to use ISO 8601 (YYYY-MM-DD) dates or DateTime::createFromFormat() when possible.

PHP- Convert String to Datetime Format

Use DateTime class to call function createFromFormat static function

$myDateTime = DateTime::createFromFormat('d/m/Y H:i', '15/09/2015 12:00');
$newDateString = $myDateTime->format('Y-m-d H:i');

Tested and giving Output:

2015-09-15 12:00

PHP - Date Format to include a string

This should do the trick...

$date->format('l jS F Y \a\t g:ia');

You need to escape characters that are not intended to be used for date values.

Note that if you're using " instead of ' for your format string you'll need to double escape n, t and r characters as PHP will interpret them as newlines, tabs etc...

For example:

$date->format("l jS F Y \a\\t g:ia");

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 format this string to a Date

try this ..

<?php

$time = strtotime('2016-04-15 14:20:00');
echo date('m/d g:ia', $time);

?>

Second Way

<?php

$format = 'Y-m-d H:i:s';
$date = DateTime::createFromFormat($format, '2016-04-15 14:20:00');
echo $date->format('m/d g:ia');

?>

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 :-)

Format a date string in PHP

As NullUserException mentioned, you can use strtotime to convert the date strings to timestamps. You can output 'intelligent' ranges by using a different date format for the first date, determined by comparing the years, months and days:

$date1 = "2011/07/01";
$date2 = "2011/07/11";

$t1 = strtotime($date1);
$t2 = strtotime($date2);

// get date and time information from timestamps
$d1 = getdate($t1);
$d2 = getdate($t2);

// three possible formats for the first date
$long = "j F Y";
$medium = "j F";
$short = "j";

// decide which format to use
if ($d1["year"] != $d2["year"]) {
$first_format = $long;
} elseif ($d1["mon"] != $d2["mon"]) {
$first_format = $medium;
} else {
$first_format = $short;
}

printf("%s - %s\n", date($first_format, $t1), date($long, $t2));

Reformatting a datetime field into a more readable format

If you want to get the actual GMT time then you would have to use an offset from GMT based on the user's timezone.

If you just want to "call" it GMT then you can get your string like this...

<?php
$date = date("F jS, Y - h:i A", strtotime($row['column-name']));
echo $date.' GMT';
?>


Related Topics



Leave a reply



Submit