Jump to content


Photo

Handling Dates in PHP


  • Please log in to reply
2 replies to this topic

#1 centenial

centenial
  • Members
  • PipPipPip
  • Advanced Member
  • 59 posts

Posted 09 October 2006 - 02:19 PM

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!

#2 miseleigh

miseleigh
  • Members
  • PipPipPip
  • Advanced Member
  • 63 posts

Posted 09 October 2006 - 03:37 PM

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.


#3 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 09 October 2006 - 04:23 PM

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.
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users