|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
Ajax Application Generator Generate database and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!
|
|
#1
|
|||
|
|||
|
Dates in PHP
ive been thinking of making a small script to handle my things to do for each day, instead of writing them down on paper, and losing it, since i have a bad memory.
i actually havent started the script yet, but theirs this question in the back of my head that ive gotta get out before i start it. Its about dates in php. Lets say in a db i have a field inside a table which contains the date the entry was made. what i want to know is how do i query the db in such a way that todays date, and all days before that, are displayed im thinking something like this mysql_query("SELECT * FROM testtable WHERE date <=$todaysdate") would this work, (the var $todaysdate would equal 2daysday, if you didnt get the idea their) thanks in advance |
|
#2
|
|||
|
|||
|
My gut feeling is your example will work, but as a habit, I usually convert all dates to timestamps before doing calculations on them. It may be overkill, but it's kept me out of trouble before.
|
|
#3
|
|||
|
|||
|
Me too, sore the time in timestamp , and with PHP functions, its easily convertable to other formats
__________________
Rathaur ====================== Knowledge is Power |
|
#4
|
|||
|
|||
|
what exactly is a timestamp??? to get the time i just use gmtime()??
is their a better way?? can you please explain about his timestamp thanks |
|
#5
|
|||
|
|||
|
a time stamp is the number of seconds or milliseconds passed since the Unix Epoch (1-1-1970). Best would be if you insert a date using PHP with (for example):
Code:
$query = "insert into timetable values(date('Y-m-d'))";
or something like that... |
|
#6
|
|||
|
|||
|
For instance, when you store the date in your MySQL database, you might do it like so:
mysql_query("INSERT INTO testtable SET date=CURDATE(), task='What to do today'"); This, of course, inserts the current date in the "date" column. Then, as you suggested in your initial question, you SHOULD be able to retrieve the data thus: $result=mysql_query("SELECT * FROM testtable WHERE date<=CURDATE() AND task IS NOT NULL"); I haven't tested it, but it should work. An alternative uses timestamps. Consider (in PHP): $date = time(); $task = "What to do today"; mysql_query('INSERT INTO testtable SET date="$date", task="$task"'); Then, to retrieve your list, you could use: $date = time(); $result=mysql_query('SELECT * FROM testtable WHERE date<="$date" AND task IS NOT NULL'); while ($answer = mysql_fetch_array($result)) { $timestamp=$answer['date']; $fdate=date("D M j, Y",$timestamp); echo "Date: ".$fdate." Task: ".$answer['task']."<br>\n"; } Again, this method could be overkill. Your original choice is more elegant if it works. |
|
#7
|
|||
|
|||
|
thanks for your suggestions guys, ill start programing it soon, and let you know how it goes
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > PHP Development > Dates in PHP |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|