Jump to content

str_to_date...


rsammy

Recommended Posts

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

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

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

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.