Re: strtotime problem

From: Date: Fri, 10 Oct 2008 17:50:27 +0000
Subject: Re: strtotime problem
References: 1 2 3 4 5 6 7 8  Groups: php.general 
Request: Send a blank email to php-general+get-281688@lists.php.net to get a copy of this message
Greetings, Thodoris. In reply to Your message dated Wednesday, October 8, 2008, 16:35:40,
Greetings to you too :-) .
From your function name I assume you want to use it in MySQL. In that case, why don't you have MySQL do all the magic for you? eg. INSERT INTO table (col) VALUES (FROM_UNIXTIME($timestamp)); (using FROM_UNIXTIME($timestamp) will give you the date-time in "mysql format" (YYYY-MM-DD HH:MM:SS) or any other format (if given via the 2nd parameter)
      
Well actually the basic question was if there is about how to change the user input rather then how to retrieve the data. As for the retrieved data: Those functions belong to framework and by changing just a function I can retrieve whatever format I need by changing a function instead of changing all the queries in the project. So I guess I will stick with that although I am aware of the date-time functions that mysql has available.
Well I have made these two functions to solve this:
    
function dateMysqlToWeb($mysqldate){
$format = 'd/m/Y'; $timestamp = strtotime($mysqldate);
return date($format,$timestamp);
}
Use SQL DATE_FORMAT instead. Remember: Request the data you need, not the data stored.
BTW it was a good suggestion but that is not always true.
function dateWebToMysql($webdate){
    
$format = 'Y-m-d'; $date_part = explode("/",$webdate); $timestamp = mktime(0,0,0,$date_part[1],$date_part[0],$date_part[2]);
return date($format,$timestamp);
}
My basic problem was how to make the user input from a form into a timestamp not how to use it with mysql. Since the format used here is dd/mm/yyyy I can't use the strtotime directly bacause as far as I know from the documentation I don't think that it changes with the timezone (as Stut suggested). So since strtotime understands all these variations like:
mm/dd/yyyy, m/d/yy, mm/d/yy etc
Very basic solution: use 3 editboxes and short javascript sample to confirm input. Like [__]/[__]/[____] <span id="timeconfirm">...</span> And fill 'timeconfirm' in onchange/onkeypress event, using verbal format like Tue, Jan 03, 2008 That way, user actually see which date s/he have entered and you may use it straightforward.
This is possible since strtotime accepts many formats and if one of them was dd/mm/yyyy it would work like charm. Nevertheless I made a javascript to chose the date directly from a window by just clicking it. In addition to that I will have to validate the form data before the insert no matter what javascript does.
I can't use this flexible behavior to get the input if it is not in the American format (and correct me if I am wrong). If I explode I will have to use the fixed format dd/mm/yyyy and I can't use other input formats beside this so I lose this flexible approach.
    
It does work for me now but I was wondering if there was a way to avoid this.
    
Not as I know. So, you should either force user to use specific format (military YYYY-MM-DD HH:MM:SS for example) or leave user no chance to enter wrong data.
I am already doing that but it would much more convenient if the format we usually use here (dd/mm/yyyy) was acceptable depending on the timezone as Stut suggested somewhere in this thread.

« previous php.general (#281688) next »