tags:

views:

223

answers:

3

it's amzing when i send a null value in date() as a second parameter then it returns some time? how to remove this thing. i want that if a string is null or empty then doesn't do anything

    $x=strtotime();
    var_dump($x);
    var_dump($x==NULL);
    echo date('H:i',$x);

display

 Warning: strtotime() expects at least 1 parameter, 0 given in 
 D:\xampp\htdocs\test\index.php on line 1
 bool(false) bool(true) 05:30
+1  A: 

Why not check the value of $x?

if($x != NULL)
    echo date('H:i',$x);
Luca Matteis
I M USING date() in mysql query, how can i do this inside mysql query
diEcho
Do your check before executing the query?
kemp
no, actually i m inserting like `insert into tbl_x(rank,open_time) in values('1',date('H:i',$x)),('2',date('H:i',$y)),('3',date('H:i',$z));` where open_time is time type field in mysql and $y and $z have some value
diEcho
Well you are calling that query from your script, right? Then **before** calling it, check that `$x` `$y` and `$z` contain correct values -- if they don't stop the execution with an error or whatever you prefer.
kemp
+1  A: 

The second parameter to the date() function is optional: if it's not provided, date() uses the current time. If you want to prevent this you have to check your variable before calling it, or write a wrapper function and use that instead of date().

kemp
+1  A: 

It makes perfect sense. NULL equals 0 in this case. date() counts from 0, which was January 1st, 1970, 0:00:00 UTC.

You are based in Jaipur. Your timezone is UTC + 5:30 hours. Thus, date(0) on your server, in your time zone, will result in January 1st, 1970, 5:30.

Pekka
so how to do sir?
diEcho