Jump to content

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

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.