Jump to content

Recommended Posts

First of all, there is a unit and reference for dates and times called a "time stamp."  A time stamp is simply the number of seconds since a reference point.  On Unix systems, this reference point is January 1, 1970.

 

In PHP, there is a function called "time()" that returns a Unix time stamp of "now," the time at which it's called.  For example, I just ran time() a moment ago and got 1223854877.  In other words, there have been about 1.2 billion seconds since 1/1/1970.

 

Now that you know what a time stamp is, you can use the function mktime() function give you the time stamp of a given date.  For example, here is how you get the time stamp of February 23, 1999, 11:35:00 AM:

 

<?php
echo mktime(11,35,00,2,23,1999);
/* returns "919787700" which is the no. of seconds between 
   Jan 1, 1970 midnight and Feb 23,1999, 11:35:00 AM */
?>

 

From there, all you have to do is find the time stamp of both dates, and convert seconds into days (I assume you know how to do division).

Let me explain:

 

strtotime() is similar to mktime() except that it allows you to input the date as strtotime(mm-dd-yyyy) instead of mktime(hour,minute,second,month,day,year).  They both return timestamps, i.e. the number of seconds since Jan.1, 1970.  When you subtract them, you get the number of seconds between the two dates, which is ok.  This is useful.

 

HOWEVER, date() does not convert seconds to days.  It converts a time stamp to a DATE.  In other words, if you gave date() a time stamp of 86401 (the number of seconds in 1 day plus 1 second), it will calculate to Jan 2, 1970.  The first argument of date() is the FORMAT of the date.  The letter "d" refers to the day portion of the date, which in the case of the time stamp 86401 is "2."

 

If the dates are significantly spread out, or straddles more than one month, xtopolis' method will not work.

 

Please read:

date(): http://us.php.net/date

time(): http://us.php.net/time

mktime(): http://us.php.net/mktime

sorry to wake up a solved old thread but i got a another question...

now that i have my sql database populated with events timestamped with unixtime,

is there a way i can use a sql command to select rows based on the day of week?

i would assume there would have to be a built in time function..... any ideas?

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.