Recipe 3.5. Finding the Difference of Two Dates3.5.1. ProblemYou want to find 3.5.2. SolutionConvert both dates to epoch timestamps Calculating the difference between two dates
Example 3-13 prints: The two dates have 128 weeks, 6 days, 14 hours, 3 minutes, Note that the difference isn't divided into larger chunks than weeks (i.e., months or years) because those chunks have variable length and wouldn't give an accurate count of the time difference calculated. 3.5.3. DiscussionThere are a few strange things going on here that you should be aware of. First of all, 1962 and 1965 precede the beginning of the epoch. Fortunately, mktime( ) Next, a wall clock (or calendar) reflects a slightly different amount of time change between these two dates, because they are on different sides of a DST switch. The result subtracting epoch timestamps gives is the correct amount of elapsed time, but the perceived human time change is an hour off. For example, on the Sunday morning in April when DST is activated, what's the difference between 1:30 A.M. and 4:30 A.M.? It seems like three hours, but the epoch timestamps for these two times are only 7,200 seconds apart'two hours. When a local clock springs forward an hour (or falls back an hour in October), the steady march of epoch timestamps takes no notice. Truly, only two hours have passed, although our clock manipulations make it seem like three. If you want to measure actual elapsed time (and you usually do), this method is fine. If you're more concerned with the difference in what a clock says at two points in time, use Julian days to compute the interval, as discussed in Recipe 3.6. To tell a user the elapsed time since her last login, you need to find the difference between the login time and her last login time, as shown in Example 3-14. Finding elapsed time since last login
3.5.4. See AlsoRecipe 3.6 to find the difference between two dates with Julian days; Recipe 3.10 for adding to and subtracting from a date; documentation on MySQL's UNIX_TIMESTAMP( ) function can be found at http://www.mysql.com/doc/D/a/Date_and_time_functions.html. |
Monday, January 11, 2010
Recipe 3.5. Finding the Difference of Two Dates
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment