softbless
asked on
Convert datetime
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?
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?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
The most important thing is the format supplied to PHP -> in this case:
j F Y g:i:s
Hope that helps :)
j F Y g:i:s
Hope that helps :)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.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);
ASKER
@Ray, ok, i'll wait
@netbuzz, it's a variable, so is it like this :
$modifiedtemp = new DateTime($textsample);
$modified=$modifiedtemp->f ormat('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\gfortun e\upload_f ile.php:78 Stack trace: #0 C:\xampp168\htdocs\gfortun e\upload_f ile.php(78 ): DateTime->__construct('13/ 12/2008 18:1...') #1 {main} thrown in C:\xampp168\htdocs\gfortun e\upload_f ile.php on line 78
@netbuzz, it's a variable, so is it like this :
$modifiedtemp = new DateTime($textsample);
$modified=$modifiedtemp->f
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\gfortun
ASKER
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 ??
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 ??
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
https://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
Yes, of course. Please, please read the article here, including the manual pages referred to.
https://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);
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
@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
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
@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.
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));
ASKER
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.
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.
Hope that helps :)
Open in new window