Jump to content

calculate days?


dadamssg

Recommended Posts

i want to calculate how many days are between today and a date sepecified. How would i could that to get a number of days...I want to enter that number in my sql query to pull up events that are going on on that day...i can't figure out how to just enter the date into the sql query so im going about it this way

 

heres the code i got to find out tomorrows events...im just gonna replace the 1 with the number of days and hope it works

 

SELECT * FROM test WHERE DATE(start) <= DATE_ADD( CURDATE(), INTERVAL+1 DAY) AND DATE(end) >= DATE_ADD( CURDATE(), INTERVAL +1 DAY) ORDER BY start DESC	

Link to comment
https://forums.phpfreaks.com/topic/143422-calculate-days/
Share on other sites

A work around would be this:

 

<?php
$sql = "SELECT * FROM test WHERE DATE(start) <= DATE_ADD( CURDATE(), INTERVAL+1 DAY) AND DATE(end) >= DATE_ADD( CURDATE(), INTERVAL +1 DAY) ORDER BY start DESC";

$query = mysql_query($sql);

while($row = mysql_fetch_assoc($query)) {
    $start = explode(" ", $row['start']);
    $start = explode("-", $start[1]);
    $end = explode("-", $row['end']);
    $end = explode("-", $end[1]);
    $start = $start[2] . "/" . $start[1] . "/" . $start[0];
    $end = $end[2] . "/" . $end[1] . "/" . $end[0];

    $start = strtotime($start);
    $end = strtotime($end);

    $between = round( abs( $start - $end ) / 86400 );
    echo date("d", $between) . " Days between";
}
?>

 

I am not sure if that will work at all. I cannot remember if that is the syntax. Give it a try and see. If you can figure it out with MySQL, that would be the preferred method as it would be faster than strtotime which is extremely slow.

Link to comment
https://forums.phpfreaks.com/topic/143422-calculate-days/#findComment-752325
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.