Welcome Guest, Not a member yet? Register   Sign In
date('Y-m-d') don't work !!! [resolved]
#1

[eluser]krislec[/eluser]
Hi guys.

I don't understand why ma date is wrong:

I use for my varaible:
$date = date('Y-m-d',strtotime($this->input->post('my_date')

echo $date gives me:

Y-d-m !!! but I'm waiting Y-m-d....


A french Guy.

Thanks.
#2

[eluser]Sudz[/eluser]
What is the value of $this->input->post(‘my_date’); is it posted from form?
Post your code.
#3

[eluser]lontong balap[/eluser]
you should check your 'my_date' format
#4

[eluser]krislec[/eluser]
the view:

Code:
...
<td>
  &lt;?php if(isset($r->date_age) AND $r->date_age!= "0000-00-00" ): echo date('d/m/Y',strtotime($r->date_age));endif; ?&gt;
</td>

the controller:
Code:
...
$this->form_validation->set_rules('date_age','Date de naissance','trim|required|xss_clean');

    if ($this->form_validation->run())
              {
                  $data = array (
                      ...
                      'date_age' => date('Y-m-d',strtotime($this->input->post('date_age'))),
                      ...

I need to put date('m/d/Y') to have a real date with this format d/m/Y (for the screen)...???


I have made little test function but i don't how I can make it in use in the set->rules line.

Code:
/* Date au format 0000-00-00 */

function toDateMysql($date){
        if (preg_match("#(\d{2})-(\d{2})-(\d{4})#",$date)) {
                return preg_replace("#(\d{2})-(\d{2})-(\d{4})#","$3-$2-$1", $date);
        } else {
                return false;
        }
}

/* Date au format 00/00/0000 */

function toDateFr($date){
        if (preg_match("#(\d{4})-(\d{2})-(\d{2})#",$date)) {
                return preg_replace("#(\d{4})-(\d{2})-(\d{2})#","$3-$2-$1", $date);
        } else {
                return false;
        }

Thanks for helping me.
#5

[eluser]johnpeace[/eluser]
Doesn't the format string have to be in double quotes?
#6

[eluser]danmontgomery[/eluser]
[quote author="johnpeace" date="1304526900"]Doesn't the format string have to be in double quotes?[/quote]

No.

I'm guessing what's happening here is that you're inputting the date as dd/mm/yyyy, and strtotime() thinks the date is in "american" mm/dd/yyyy format, as it's wont to do.

see: http://www.php.net/manual/en/function.st...php#100144

I would force - rather than /, replace the characters before converting format, or use something like jquery datepicker
#7

[eluser]krislec[/eluser]
Ok.
I'm working on it...

THKS.

Ok great ! I undrestand :

Quote:Forward slash (/) signifies American M/D/Y formatting, a dash (-) signifies European D-M-Y
#8

[eluser]krislec[/eluser]
I'm going to try with jquery datepicker, but...

How can I make this works (for example and if I need to do this) ?

...
$this->form_validation->set_rules('date_age','Date de naissance','trim|test_my_date|required|xss_clean');

Where I have to put my code (function test_my_date() in ?
A new helper that I have to include in /application/helpers ?
#9

[eluser]krislec[/eluser]
Ok. That's done.
It works fine with "-" rather than "/", with or whithout date picker.

Thanks.

K.

ps => Resolved !




Theme © iAndrew 2016 - Forum software by © MyBB