0

I'have a problem in my Sql query in PHP:

If I do :

$result = mssql_query("SELECT xx FROM x JOIN x ON x WHERE x = '".$var1."' 
AND x BETWEEN '20150401 00:00:00' AND '20150401 23:59:59' ")

That's work

But if I do :

$day=date("d/m/Y",time());
$exploDate = explode("/", $day);
$dateStart = $exploDate[2].$exploDate[1].$exploDate[0]." 00:00:00";
$dateEnd = $exploDate[2].$exploDate[1].$exploDate[0]." 23:59:59";
$result = mssql_query("SELECT xx FROM x JOIN x ON x WHERE x = '".$var1."' 
AND x BETWEEN '".$dateStart."' AND '".$dateEnd."' ")

That's doesn't work !

I don't understand why
Do you have any idea ?

Thanks

3
  • Do you really mean "FROM x JOIN x"? And why the name mssql_query if it's MySQL - very confusing. Commented Apr 1, 2015 at 14:40
  • Echo out or var_dump your variables, one or more of them probably isn't what you think it is. Commented Apr 1, 2015 at 14:42
  • Try using echo $dateStart and echo $dateEnd before calling your query to see if the values you're entering are correct. Commented Apr 1, 2015 at 14:43

1 Answer 1

2

KISS Principle.

date('Y-m-d 00:00:00')."' AND '". date('Y-m-d 23:59:59')

or

$today = date('Y-m-d');
// ...
... "BETWEEN '" . $today . " 00:00:00' AND '" . $today . " 23:59:59'"
Sign up to request clarification or add additional context in comments.

2 Comments

Would that work with the date formatting from the first query? It seems his original format has the year, month and day jammed together with no separation.
there is a manual for date() function. You can use date("Ymd") as well as - once you read manual - even something to produce "2015 is a year of lovers!" if you want: echo date('Y \Y\e\a\r \o\f \l\o\v\e\r\s');.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.