rsammy Posted March 12, 2007 Share Posted March 12, 2007 i am trying to compare two dates. one comes as a string (1/29/2007) and the other comes in as a Date field (02/05/2007). when i try to compare these two dates, it fails as one is a string without a leading zero for the month and the other is not. how do i comapre these? is there a way i can convert the string into a date format and then compare? i saw somewhere that str_to_date can be used. but im not sure how to use it here. my query is ... $selectadmissionquery1=("SELECT id, pat_id, admit_status, admit_loc, Date_Format(admit_date, '%m/%d/%Y') as admit_date, Date_Format(discharge_date, '%m/%d/%Y') as discharge_date, admit_encounter FROM admission WHERE pat_id = '$pat_id' AND admit_date >= '$dov' AND discharge_date >= '$dov' ORDER BY admit_date ASC limit 1"); in the above query... admit_date is from the database and in the format mm/dd/yyyy. $dov is the date that comes in as a string(m/dd/yyyy). thanx Link to comment https://forums.phpfreaks.com/topic/42397-str_to_date/ Share on other sites More sharing options...
kenrbnsn Posted March 12, 2007 Share Posted March 12, 2007 Use the function strtotime() to convert both dates to a UNIX timestamp and then compare the two timestamps. Ken Link to comment https://forums.phpfreaks.com/topic/42397-str_to_date/#findComment-205673 Share on other sites More sharing options...
rsammy Posted March 12, 2007 Author Share Posted March 12, 2007 thanx a lot for replying. but how do i do this? Link to comment https://forums.phpfreaks.com/topic/42397-str_to_date/#findComment-205679 Share on other sites More sharing options...
kenrbnsn Posted March 12, 2007 Share Posted March 12, 2007 This code snippets gives two examples of comparing dates: <?php $test_date1 = '1/29/2007'; $test_date2 = '02/05/2007'; $test_ts1 = strtotime($test_date1); $test_ts2 = strtotime($test_date2); if ($test_ts1 == $test_ts2) echo 'Dates are the same'; // comparing using an "if" statement else echo 'Dates are not the same'; echo '<br>'; switch (true) { // comparing using a "switch" statement case $test_ts1 < $test_ts2: echo $test_date1 . ' is earlier than ' . $test_date2; break; case $test_ts1 > $test_ts2: echo $test_date1 . ' is later than ' . $test_date2; break; case $test_ts1 == $test_ts2: echo $test_date1 . ' is equal to ' . $test_date2; break; } ?> Ken Link to comment https://forums.phpfreaks.com/topic/42397-str_to_date/#findComment-205696 Share on other sites More sharing options...
rsammy Posted March 12, 2007 Author Share Posted March 12, 2007 the date in string format ("1/29/2007") is the input i receive from my mobile device. where as the date in i'm trying to compare it with (02/05/2007) is in the database. now, how can i convert the date field in the database before i retrieve it in the query?! is there a way where i can convert the input (string) date to a proper date format and then compare the two fields? would appreciate if you could help me with that! Link to comment https://forums.phpfreaks.com/topic/42397-str_to_date/#findComment-205699 Share on other sites More sharing options...
kenrbnsn Posted March 12, 2007 Share Posted March 12, 2007 My post above should give you enough information to solve your problem. What don't you understand? Ken Link to comment https://forums.phpfreaks.com/topic/42397-str_to_date/#findComment-205702 Share on other sites More sharing options...
rsammy Posted March 12, 2007 Author Share Posted March 12, 2007 well, im not too sure about that! thanx for your reply anyways! appreciate it Link to comment https://forums.phpfreaks.com/topic/42397-str_to_date/#findComment-205785 Share on other sites More sharing options...
rsammy Posted March 13, 2007 Author Share Posted March 13, 2007 figured it out, thanx for ur help! Link to comment https://forums.phpfreaks.com/topic/42397-str_to_date/#findComment-206607 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.