davids701124 Posted October 20, 2009 Share Posted October 20, 2009 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. Quote Link to comment Share on other sites More sharing options...
Mark Baker Posted October 20, 2009 Share Posted October 20, 2009 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) Quote Link to comment Share on other sites More sharing options...
davids701124 Posted October 20, 2009 Author Share Posted October 20, 2009 ... 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?? Quote Link to comment Share on other sites More sharing options...
Mark Baker Posted October 20, 2009 Share Posted October 20, 2009 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 Quote Link to comment Share on other sites More sharing options...
davids701124 Posted October 20, 2009 Author Share Posted October 20, 2009 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>"; Quote Link to comment Share on other sites More sharing options...
Mark Baker Posted October 20, 2009 Share Posted October 20, 2009 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.