CodeIgniter Forums
Date convert from SQL - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22)
+--- Thread: Date convert from SQL (/showthread.php?tid=56632)



Date convert from SQL - El Forum - 01-08-2013

[eluser]Chilion[/eluser]
Hi!

I have a date from a sql DATE field.

Then I want to turn that into my preffered format. I use the mdate function now in this way:
Code:
<?php
      $datestring = '%d-%m-%Y';
      echo mdate($datestring, $statistic->creationDate);
     ?>

Then I get an error:
Quote:Fatal error: Call to undefined function mdate() in C:\USBWebserver\root\Dweper\frameworkSysteemCodeSecure\core\Loader.php(829) : eval()'d code on line 24

$statistic->creationDate is filled as: 2012-01-04

Hope u can help!



Date convert from SQL - El Forum - 01-08-2013

[eluser]PhilTem[/eluser]
There's no native PHP mdate function but it's a CI helper function. So you should ensure, that you loaded the date-helper prior to calling the method.

Secondly, I think you're not using the write markup. From the user's guide
Quote:
Code:
$datestring = "Year: %Y Month: %m Day: %d - %h:%i %a";
$time = time();

echo mdate($datestring, $time);

In this case, $time is something like 123456789. And that's not matching your format Wink


Date convert from SQL - El Forum - 01-08-2013

[eluser]Chilion[/eluser]
What should I use to do it the way I want it then?


Date convert from SQL - El Forum - 01-17-2013

[eluser]Unknown[/eluser]
You can use native PHP code

Code:
<?php
      $datestring = 'd-m-Y';
      echo date($datestring, strtotime($statistic->creationDate));
     ?>

http://php.net/manual/en/function.date.php


Date convert from SQL - El Forum - 01-23-2013

[eluser]Ajaxboy[/eluser]
Code:
<?php
$time = strtotime($statistic->creationDate);
echo date("d-m-Y", $time );
?>


Date convert from SQL - El Forum - 01-23-2013

[eluser]CroNiX[/eluser]
I prefer to do the date conversion in the actual query using DATE_FORMAT() so it returns the format I want from the getgo.
http://www.w3schools.com/sql/func_date_format.asp