Jump to content

which function is for calculate how long between date and date


davids701124

Recommended Posts

I have 2 dates, says 2010-11-24 and 2009-10-12

I wanna get the each date which between this duration belong to which week which day.

So I think I have to sub them and divide 7, but no idea which function can convert them right.

i check php reference, but not sure how to do it right?

 

hope someone help, thank u.

strtotime() to convert the string dates to a date/timestamp, then just subtract the two values to get the difference... but you'd need to divide them by a bit more than just 7, because the value of a date/timestamp is in seconds, so divide by (7 * 24 * 60 * 60)

... but you'd need to divide them by a bit more than just 7, because the value of a date/timestamp is in seconds, so divide by (7 * 24 * 60 * 60)

 

does this issue exist, even I was using date rather than datetime or timestamp??

Where are you getting the dates from?

If they are from a database, and the column datatype is a date, then you could do the math in your SQL query

SELECT date1, date2, datediff(date2,date1) / 7 as weeks
  FROM table

If the values are strings (either input from an html form, or held on the database as VARCHAR2), then you need to convert them to a date/timestamp

 

this is what I did until now, but after subtract the value is 0...weird??

 

$duedate = mysql_query( "SELECT due_date FROM baby WHERE baby_id = '29'" );

while ( $row = mysql_fetch_assoc($duedate)){

echo strtotime($row["due_date"])."<br>";

}

 

$regi_date = mysql_query("SELECT user_registered FROM user WHERE user_id ='43'");

while ( $row = mysql_fetch_assoc($regi_date)){

echo strtotime($row["user_registered"])."<br>";

}

 

$a = strtotime($row["due_date"]);

$b = strtotime($row["user_registered"]);

echo $a - $b."<br>";

this is what I did until now, but after subtract the value is 0...weird??

Not strange if strtotime() fails because the values are already dates.

 

Try this database query:

SELECT b.due_date as due_date,
       u.user_registered as user_registered,
       (b.due_date - u.user_registered) / 7 as weeks
  FROM baby b,
       user u
WHERE b.baby_id = 29
   AND u.user_id = 43

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.