Jump to content

[SOLVED] Days Passed


KingOfHeart

Recommended Posts

I agree with mark baker. You should really use a timestamp. With dates you can use the built in php functions to achieve what you would like to do. If you use this method what happens if it is a leap year. Then your calculations will be off. With the built in php functions it will take that into account.

Link to comment
Share on other sites

I agree with mark baker. You should really use a timestamp. With dates you can use the built in php functions to achieve what you would like to do. If you use this method what happens if it is a leap year. Then your calculations will be off. With the built in php functions it will take that into account.

 

 

Errr.... I think he meant a MySQL datetime column (or a date column).

Link to comment
Share on other sites

What php command can I use then?

 

$lu = "20081103224334";

$year = $lu[0] . $lu[1] . $lu[2] . $lu[3];

$month = $lu[4] . $lu[5];

$day = $lu[6] . $lu[7];

 

I can easily move around the numbers in any order that is needed, guys.

 

edit: I found exactly what I needed.

 

$lu = "20081103224334";
$year = $lu[0] . $lu[1] . $lu[2] . $lu[3];
$month = $lu[4] . $lu[5];
$day = $lu[6] . $lu[7];
$time = time() - mktime(0, 0, 0, $month, $day, $year);
$days = floor($time/86400);
echo $days;

 

I knew it was not that hard to do, so don't see why you did not suggest this for me. Sorry if I sound harsh guys.

 

*Thread Closed*

Link to comment
Share on other sites

Sorry about that. Read that wrong. I always store my dates as a timestamp in (int) format. You can pretty much do whatever you want with them then, pretty easily.

 

 

I used to use unix timestamps for stuff too, and then one day in the MySQL forum, it smacked me in the face.

 

I'm sure there are other things too, but for example, let's say you're making a hit counter...  How would you find hits on Thursdays?

Link to comment
Share on other sites

I think you are wrong but then again I could be wrong lol. but wouldnt this give you the day of the week in numeric form:

 

$day_of_week = date("w", $timestamp);

 

Then you could simply compare that to whatever the day was you wanted to compare it to.

 

 

Sorry about that. Read that wrong. I always store my dates as a timestamp in (int) format. You can pretty much do whatever you want with them then, pretty easily.

 

 

Good luck comparing a string representation of a week index to a unix timestamp.

Link to comment
Share on other sites

ok let me give you an example that maybe you can understand what I am saying. Here it goes:

 

<?php
//make the database connection
$connect = mysql_connect("host", "user", "pass");

//select database
mysql_select_db("database");

//make the query
$query = "SELECT timestamp FROM table";

//perform the query
$results = mysql_query($query);

//the date thursday has a numeric value of 4

//initialize counter
$counter = 0

//compare against database for hit counter
while($row = mysql_fetch_array($results))
{
$day_of_week = date("w", $row['timestamp']);
if ($day_of_week == "4")
{
$counter++
}
}

echo $counter

?>

 

Doesn't that do what you are trying to do by getting the hits for that day?

Link to comment
Share on other sites

Do me a favor.

 

Fill a table with 1,000,000 random rows, run that script, and tell me how long it takes.

 

 

Then tell me how long this would take:

 

 

SELECT COUN(*) FROM hits GROUP BY DAYOFWEEK(timestamp);

 

 

 

(Of course the fastest solution would be to pre store the day of the week, but we're assuming that situation hasn't been done.)

Link to comment
Share on other sites

Just want to make a note of your initial statement:

 

I'm sure there are other things too, but for example, let's say you're making a hit counter...  How would you find hits on Thursdays?

 

You said how would you find hits on a thurday. I showed you how. You were acting like it isn't possible and I was just letting you know that it is possible. I never said that it was the fastest or the best method but it is possible.

Link to comment
Share on other sites

Yeah, that's what I was alluding to when I said it's possible, but why have to convert it when you could just store it in what you're going to convert it to?  It would of course be easier to use in PHP as a timestamp though, so in some cases I guess it would make more sense to store it in a timestamp format.  Only when not manipulating it in MySQL though.

Link to comment
Share on other sites

I would be willing to bet that those queries did not convert it to a datetime column ;p.  But yeah, a timestamp can of course be better in some situations.  I would probably use an integer timestamp for a registration time, for example (unless I ever planned on doing stats off of the registration times...).

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.