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.

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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>";

Link to comment
Share on other sites

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

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.