petenetman Posted July 22, 2010 Share Posted July 22, 2010 I’m using this string $oDate = strtotime($row['CompletedIT']); $sDate = date(“d/m/y”,$oDate); echo $sDate to get the MYSQL date value stored in the table row ‘CompletedIT’. The database value is 2010-07-22 but I get the echo return of 01/01/70 Can someone give me an idea as I want to display the date as dd/mm/yyyy Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/208553-php-dates-from-mysql-yyyy-mm-dd-to-dd-mm-yyyy/ Share on other sites More sharing options...
Pikachu2000 Posted July 22, 2010 Share Posted July 22, 2010 Just use the DATE_FORMAT() MySQL function in the query string and return the date pre-formatted. SELECT DATE_FORMAT(`field_name`, '%d/%m/%Y') AS `formatted_date` . . . Quote Link to comment https://forums.phpfreaks.com/topic/208553-php-dates-from-mysql-yyyy-mm-dd-to-dd-mm-yyyy/#findComment-1089641 Share on other sites More sharing options...
radar Posted July 22, 2010 Share Posted July 22, 2010 The way I do it is like: $now = date('Y-m-d'); $oned = strtotime("$now +1 day"); $oned = date('d/m/Y', $oned); so for your solution it would be $oDate = strtotime("$row[CompletedIT]"); $sDate = date("d/m/Y, $oDate); echo $sDate; that should work theoretically. Quote Link to comment https://forums.phpfreaks.com/topic/208553-php-dates-from-mysql-yyyy-mm-dd-to-dd-mm-yyyy/#findComment-1089642 Share on other sites More sharing options...
Mchl Posted July 22, 2010 Share Posted July 22, 2010 Or using your code, change format to 'd/m/Y' Quote Link to comment https://forums.phpfreaks.com/topic/208553-php-dates-from-mysql-yyyy-mm-dd-to-dd-mm-yyyy/#findComment-1089643 Share on other sites More sharing options...
petenetman Posted July 22, 2010 Author Share Posted July 22, 2010 Using $oDate = strtotime($row["CompletedIT"]); $sDate = date('d/m/y',$oDate); echo $sDate gives me the echo return of 01/01/70 when the date stored is 2010-07-22 Does the 01/01/70 indicate something like PHP can't find the recor? I've been on this all day now and I think I am going round in circles. Quote Link to comment https://forums.phpfreaks.com/topic/208553-php-dates-from-mysql-yyyy-mm-dd-to-dd-mm-yyyy/#findComment-1089647 Share on other sites More sharing options...
radar Posted July 22, 2010 Share Posted July 22, 2010 $oDate = strtotime("$row[CompletedIT]"); $sDate = date("d/m/Y, $oDate); echo $sDate; use that and see what pops out. Quote Link to comment https://forums.phpfreaks.com/topic/208553-php-dates-from-mysql-yyyy-mm-dd-to-dd-mm-yyyy/#findComment-1089663 Share on other sites More sharing options...
Mchl Posted July 22, 2010 Share Posted July 22, 2010 Can you show the query that is supposed to return this data? It seems that $row["CompletedIT"] is either empty, or is not in MySQL's data format. Quote Link to comment https://forums.phpfreaks.com/topic/208553-php-dates-from-mysql-yyyy-mm-dd-to-dd-mm-yyyy/#findComment-1089682 Share on other sites More sharing options...
petenetman Posted July 22, 2010 Author Share Posted July 22, 2010 When I use $oDate = strtotime("$row[CompletedIT]"); $sDate = date("'d/m/y' $oDate"); echo $sDate I just get todays date displayed If I just use echo $rows["CompletedIT"]; I get the date value from the DB 2010-07-22 Quote Link to comment https://forums.phpfreaks.com/topic/208553-php-dates-from-mysql-yyyy-mm-dd-to-dd-mm-yyyy/#findComment-1089696 Share on other sites More sharing options...
PFMaBiSmAd Posted July 22, 2010 Share Posted July 22, 2010 $rows["CompletedIT"]; is not the same as $row[CompletedIT] What is your actual code that fetches the result from the query? You might want to reread the reply that Pikachu2000 made because using the mysql DATE_FORMAT() function directly in your query is both simpler and at least 8x faster than trying to do this using php code. Quote Link to comment https://forums.phpfreaks.com/topic/208553-php-dates-from-mysql-yyyy-mm-dd-to-dd-mm-yyyy/#findComment-1089698 Share on other sites More sharing options...
petenetman Posted July 22, 2010 Author Share Posted July 22, 2010 Thanks for all your help guys - It's now tested and working. What I used in the end was $oDate = strtotime("$rows[CompletedIT]"); $sDate = date("d/m/Y", $oDate); echo "UK Format:" . " " . "Task Closed on..." . " " . $sDate Quote Link to comment https://forums.phpfreaks.com/topic/208553-php-dates-from-mysql-yyyy-mm-dd-to-dd-mm-yyyy/#findComment-1089747 Share on other sites More sharing options...
PFMaBiSmAd Posted July 22, 2010 Share Posted July 22, 2010 If you were developing and debugging php code on a system with error_reporting set to E_ALL and display_errors set to ON, php would help you find things like variable names that are different than what you are actually using because they would show up as undefined variable errors due to the name mismatch. You will save a ton of time. Quote Link to comment https://forums.phpfreaks.com/topic/208553-php-dates-from-mysql-yyyy-mm-dd-to-dd-mm-yyyy/#findComment-1089781 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.