Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

add compare date time

Status
Not open for further replies.

Kurt111780

Technical User
Nov 20, 2003
235
GB
Hello,

In one script I need to get the current date and time then add a certain number of hours or days to it before storing it in the database.

Then on another page a script will compare the current date and time to the one stored in the database. if ($DBtime >= $currTime)

I was going to store the date using this format $timeStamp = date("YmdHis"); but then how can I add days or hours to it? Should I use DateTime field type in MySql?

Thanks,
Kurt

It's only easy when you know how.
 
i would use a varchar or numeric field to store the date time information and store the timestamp in the database as a unix timestamp (number of seconds elapsed since the beginning of the unix epoch). then converting all your elapsed time into seconds should make light work of the maths.

you can convert either way easily enough with strtotime() and date() functions. just be sure to be consistent in your treatment otherwise you can get valid results that are incorrect and this can take hours to debug...
 
Hello,

I guess I'll use varchar type. This works good

Code:
echo date('YmdHis', strtotime('+7 days'));
echo "<br /><br />";
echo date('YmdHis', strtotime('+3 hours'));

Kurt

It's only easy when you know how.
 
use timestamp and mysql's date_add function, its far easier

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
KarverR: is this syntax common across all/most SQL database engines?

I'm interested because personally i prefer to use db abstraction layers so that i can reuse my code for clients independent of their db engines. this leads me to keep my sql as simple as possible to maximise the likelihood of the code working cross-platform.
 
It seems to be common for mssql, pgsql, oracle and ysql .. others you'll have to figure out.

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
KarveR,

How would you use timestamp when comparing and adding like I wanted to do in my orginal post? Seems easier for me to use the php date and strtotime functions.

Kurt

It's only easy when you know how.
 
for mysql

select date_add(curdate(), interval 30 day);

this selects todays date and adds 30 days.

select date_sub(curdate(), interval 15 day);

this subtracts 15 days from today.

see the date and time functions manual page for more info, I think you'll find it very enlightening.


______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top