We help IT Professionals succeed at work.

Convert datetime

softbless
softbless asked
on
Dear All,

I have a variable $textsample with value 27/12/2008 3:49:16

I want to convert it to become : 27 December 2008 3:49:16

Could you tell me how to do it in PHP?
Comment
Watch Question

Commented:
Here's the code

Hope that helps :)
<?php
	$today = date("j F Y g:i:s"); 
	echo $today;
?>

Open in new window

Commented:
To show date specific to your case:
<?php
	$date = new DateTime('2008-12-27 3:49:16');
	echo $date->format('j F Y g:i:s');
?>

Open in new window

Commented:
The most important thing is the format supplied to PHP -> in this case:

j F Y g:i:s

Hope that helps :)
Most Valuable Expert 2011
Top Expert 2016
Commented:
The $textsample is not a valid datetime string.  I will show you a conversion algorithm that may be helpful.

Also, you might want to read the article here:
http://www.experts-exchange.com/articles/Web_Development/Web_Languages-Standards/PHP/Handling-date-and-time-in-PHP-and-MySQL.html

Back in a moment, ~Ray
Most Valuable Expert 2011
Top Expert 2016

Commented:
Here is a teaching example showing the moving parts.  You can install it and run it to see show the date() and strtotime() functions work.

PHP.net man pages are listed in the article linked above.  Worth a quick read.

best of luck with your project, ~Ray
<?php // RAY_temp_datething.php
error_reporting(E_ALL);

// TEST DATA FROM EE
$text = '27/12/2008 3:49:16';
echo "<br/>ORIGINAL TEXTUAL DATE-TIME: $text";

// CONVERT TO TIMESTAMP AND SHOW THAT IT DOES NOT WORK
$time = strtotime($text); // THIS RETURNS FALSE
if (!$time) echo "<br/>$text IS NOT A VALID DATE-TIME STRING";

// REFORMAT TEH STRING - THERE ARE LOTS OF WAYS TO DO THIS
$arr = explode(' ', $text);
$date = $arr[0];
$time = $arr[1];
$xdat = explode('/', $date);
$text = $xdat[2] . '-' . $xdat[1] . '-' . $xdat[0] . ' ' . $time;
echo "<br/><br/>CONVERTED TEXTUAL DATE-TIME: $text";

// TRY THE CONVERSION AGAIN (IT WORKS THIS TIME)
$time = strtotime($text); 
if (!$time) echo "$text IS NOT A VALID DATE-TIME STRING";

// SHOW THE ISO8601-FORMAT DATE
echo "<br/>" . date('c', $time);

// SHOW THE CLIENT-FORMAT DATE
echo "<br/>" . date('d F Y g:i:s', $time);

Open in new window

Author

Commented:
@Ray, ok, i'll wait

@netbuzz, it's a variable, so is it like this :

$modifiedtemp = new DateTime($textsample);
   $modified=$modifiedtemp->format('j F Y g:i:s');

Is it like that? Cause it give error :

Failed to parse time string (13/12/2008 18:14:46) at position 0 (1): Unexpected character' in C:\xampp168\htdocs\gfortune\upload_file.php:78 Stack trace: #0 C:\xampp168\htdocs\gfortune\upload_file.php(78): DateTime->__construct('13/12/2008 18:1...') #1 {main} thrown in C:\xampp168\htdocs\gfortune\upload_file.php on line 78

Author

Commented:
Hi Ray,

It works perfectly fine. But 2008-12-13 18:14:44 is converted into :13 December 2008 6:14:44

Could you make it to become : 13 December 2008 18:14:44 ??
all you need to do is change this

echo "<br/>" . date('d F Y g:i:s', $time);  

with


echo "<br/>" . date('d F Y H:i:s', $time); in


In ID:32990324    Author:Ray_Paseur        solution

You can convert any string in date time format using php function

$myvar = strtotime($textsample)

Now your sting is in date time format...

you can use date('d F Y H:i:s',$myvar);

and you will get your desired result.
Most Valuable Expert 2011
Top Expert 2016

Commented:
Regarding this question:  Could you make it to become : 13 December 2008 18:14:44 ??

Yes, of course.  Please, please read the article here, including the manual pages referred to.
http://www.experts-exchange.com/articles/Web_Development/Web_Languages-Standards/PHP/Handling-date-and-time-in-PHP-and-MySQL.html

The exact man page that holds the key to the format of the date/time string is located here:
http://us.php.net/manual/en/function.date.php

On that page you will see that the time on a 24 hour clock is formatted with code G or H.  You need to choose the format that makes sense for your application.  Probably one of these will do what you want (last lines of the script I posted above).

HTH, ~Ray
// SHOW THE CLIENT-FORMAT DATE
echo "<br/>" . date('d F Y G:i:s', $time);

// SHOW THE CLIENT-FORMAT DATE
echo "<br/>" . date('d F Y H:i:s', $time);

Open in new window

Commented:
Hi softbless,
Try the code that I have attached and see if you find it simple to use. Actual code processing is 3 lines and hopefully very easy to understand for you.

Try it and let me know if it worked for you.
$current_date = '2008/12/27 18:14:44';

$exp = explode(' ', $current_date);

list($year, $month, $day) = split('[/]', $exp[0]);

$revised_date = $year.'-'.$month.'-'.$day.' '.$exp[1];

echo date('d F Y H:i:s', strtotime($revised_date));

Open in new window

Most Valuable Expert 2011
Top Expert 2016

Commented:
@mepro: Regarding line 5 of the code snippet, please see the large red note on the split() function here:
http://us2.php.net/manual/en/function.split.php

I think this strategy was almost exactly what I posted above in lines 12 through 18 of the well-commented code snippet at 32990324.  But I did not use a deprecated function ;-)

In any case, our asker has enough information to move forward now.

Best regards to all, ~Ray

Commented:
@Ray,
Oh I did not realize it was deprecated. Thanks for pointing it out. Yes, I believe the information that asker has provided, does not leave us with too many options to approach it in a different manner and maybe that's the reason why our approaches may look alike. :)

@softbless,
I have modified one line in my code and I am attaching the revised code below in order to avoid using the deprecated function.
$current_date = '2008/12/27 18:14:44';

$exp = explode(' ', $current_date);

list($year, $month, $day) = explode('/', $exp[0]);

$revised_date = $year.'-'.$month.'-'.$day.' '.$exp[1];

echo date('d F Y H:i:s', strtotime($revised_date));

Open in new window

Author

Commented:
Hi Guys,

Thanks for the answer, hope I could give to you all high scores, but the score is limited, and I give higher score to the ones that responds faster.

Thanks for helping.