Jump to content

Handling Dates in PHP


centenial

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!
Link to comment
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.
Link to comment
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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.