Welcome Guest, Not a member yet? Register   Sign In
Date conversion in codeigniter
#1

[eluser]elmne[/eluser]
How do i convert a date in the format

02/05/2010 which is dd/mm/yyyy so that it can be inserted into a Mysql database?

and also retrieved and returned to the same format?

I looked at the date helper in the user guide but standard date lacks the format specified above.
#2

[eluser]Clooner[/eluser]
[quote author="elmne" date="1275895986"]How do i convert a date in the format

02/05/2010 which is dd/mm/yyyy so that it can be inserted into a Mysql database?

and also retrieved and returned to the same format?

I looked at the date helper in the user guide but standard date lacks the format specified above.[/quote]

Why not simply use the php date function to do this? You dont need any extra helpers for this! http://php.net/manual/en/function.date.php
#3

[eluser]mddd[/eluser]
Mysql is a bit flexible on how you specify the date, but the important thing is that the order must be year-month-day. If your date is already a string, you could use:
Code:
$mydate = '02/05/2010';
$this->db->set('mydate', join('-',array_reverse(explode('/',$mydate))));
In other words, make the date into an array, reverse its order, out it back together again. It doesn't really matter what you put in between the parts (a - or a /).

For getting the date FROM the database, use Mysql's date_format function. It works just like date() in php. But why process it in php if you can get it the right way from Mysql in the first place. See date_format in the Mysql manual .
#4

[eluser]elmne[/eluser]
When i try using the following function and call it to convert the date within the class


Code:
function con2mysql($date) {

  $date = explode("-",$date);
  if ($date[0]<=9) { $date[0]="0".$date[0]; }
  if ($date[1]<=9) { $date[1]="0".$date[1]; }
  $date = array($date[2], $date[1], $date[0]);

return $n_date=implode("-", $date);
}


I get the error message

Quote:Severity: Notice

Message: Undefined offset: 1
#5

[eluser]mddd[/eluser]
That's easy.. You are exploading the date using "-". The date in your example has "/" as the separator.
If you use explode() and the separator string is not found, you'll get an array that contains only 1 item.
Also, you don't need to put zeroes in the date for Mysql. '2010/6/20' is just as fine as '2010/06/20'.
#6

[eluser]Clooner[/eluser]
Simply using the date function!

Code:
$date = explode("/", $yourowntimeformat);
$unixtime = mktime(0, 0, 0, $date[1], $date[0], $date[2]);
// $unixtime = time(); // to get the current time!
echo date("Y-m-d H:i:s", $unixtime);
#7

[eluser]mddd[/eluser]
That's another solution. But I think those date operations take longer than a simple string/array operation. Why take the detour of using date functions, when all that needs to be done is to swap the order of a few characters in the string?
#8

[eluser]Clooner[/eluser]
[quote author="mddd" date="1275931656"]That's another solution. But I think those date operations take longer than a simple string/array operation. Why take the detour of using date functions, when all that needs to be done is to swap the order of a few characters in the string?[/quote]
I highly doubt that the minuscule extra time spend on the date functions is an to any concern of 90% of the CI users here on the forum. Especially if they don't know how to generate a correct mysql time. Plus you get back the saved time at the mysql end because it's the exact format mysql needs cancelling out the extra time needed for conversion there Tongue
#9

[eluser]mddd[/eluser]
Ok, maybe the difference is not important to most users. But I do think that it makes things more complicated than necessary if you first turn the numbers into a unix time, then turn the unix time into a formatted string, while you could just rearrange the string from the start and be done with it. I don't see the value in using those date functions, if you're not going to use them for anything else then a simple array reversal.
#10

[eluser]mattpointblank[/eluser]
[quote author="clooner" date="1275931257"]Simply using the date function!

Code:
$date = explode("/", $yourowntimeformat);
$unixtime = mktime(0, 0, 0, $date[1], $date[0], $date[2]);
// $unixtime = time(); // to get the current time!
echo date("Y-m-d H:i:s", $unixtime);
[/quote]

or even just:

Code:
echo date("Y-m-d H:i:s", strtotime($your_timestamp));




Theme © iAndrew 2016 - Forum software by © MyBB