Jump to content

Archived

This topic is now archived and is closed to further replies.

centenial

Handling Dates in PHP

Recommended Posts

Hi,

I need to store a download date in a MySQL Database when a user makes an order, and check to see if that download date has expired when the user logs in in the future. What would be the best way to do this? timestamp?

Is there a PHP function that can anaylze the timestamp (or some sort of date string) and check to see how much time is left?

Example: User orders download access for 24 hours - Comes back 5 hours, 20 minutes later - It should tell him that he has 18 hours fourty minutes left.

I just need help on the timestamp/php date/math function... I've got the database queries down.

Thanks!

Share this post


Link to post
Share on other sites
Try php's time() function, it'll give you a timestamp - you can store this in your db for the order time.  Then when they come back, it's just a calculation and formatting:

$time_left = time() - $order_time;
echo strftime("You have %j days, %H hours, and %M minutes left", $time_left);

There are a few things to be aware of with this script.  The %j option is the day of the year by decimal # (for example, Feb 2nd would be the 33rd day) so it will only go as high as 366.  There are more options listed to include years if you need them - check the manual.  Also, if the user orders access for a day and it crosses daylights savings time, they'll still get exactly 24 hours even though that particular day might have either 25 or 23 hours in it.

Share this post


Link to post
Share on other sites
i would suggest storing any times or dates in mysql using date, time or datetime datatypes. you can then easily transfer it to a UNIX timestamp for calculations using strtotime(). miseleigh has given you a really good starting point for your calculations. there are a couple ways that may be a little more friendly for what you're after, but it's definitely a good jumping off point for you.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.