CodeIgniter Forums
Dateformat convert - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Dateformat convert (/showthread.php?tid=26560)



Dateformat convert - El Forum - 01-17-2010

[eluser]33cent[/eluser]
Hi,

i'm using jQuery datepicker in my new project, to select date for my entry, and format of the date is dd.mm.yyyy. But, in the database, i want to use format yyyy-mm-dd

Have no idea, how to convert date format "on-the-fly" during insert.
I tried something like: "date" = $this->datemodel->convertdate($_POST['date']); but without success.


Dateformat convert - El Forum - 01-17-2010

[eluser]flaky[/eluser]
Display the date in ISO format. Produces '2007-01-26'.

Code:
$.datepicker.formatDate('yy-mm-dd', new Date(2007, 1 - 1, 26));



Dateformat convert - El Forum - 01-17-2010

[eluser]mehike[/eluser]
this is not answer to question...

right now I'm some answer to.

our country is date format - dd.mm.yyyy
but in database I must save this another way.

I use helper right now for this and this is workiing but it's not good way for this, I know...

if ( ! function_exists('date_to_db')) {
function date_to_db($dat) {
$dat_arr = date_parse($dat);
return $dat_arr['year'].'-'.$dat_arr['month'].'-'.$dat_arr['day'];
}
}

if ( ! function_exists('date_from_db')) {
function date_from_db($dat) {
$dat_arr = date_parse($dat);
if (strlen($dat_arr['month'])==1)
$dat_arr['month'] = '0'.$dat_arr['month'];
if (strlen($dat_arr['day'])==1)
$dat_arr['dat'] = '0'.$dat_arr['day'];
return $dat_arr['day'].'.'.$dat_arr['month'].'.'.$dat_arr['year'];
}
}


I't must be better way....
but I'm not time to search it.


Dateformat convert - El Forum - 01-17-2010

[eluser]Dyllon[/eluser]
Use strtotime() to get a timestamp and use it with date() to get any format you wish.


Dateformat convert - El Forum - 01-18-2010

[eluser]saidai jagan[/eluser]
Use explode
Code:
$dat_arr = explode('-',$date);
return $dat_arr[2].'-'.$dat_arr[1].'-'.$dat_arr[0];



Dateformat convert - El Forum - 01-18-2010

[eluser]danmontgomery[/eluser]
[quote author="Dyllon" date="1263781207"]Use strtotime() to get a timestamp and use it with date() to get any format you wish.[/quote]

This.

Code:
function convert_date($date)
{
  return date('d.m.Y', strtotime($date));
}