Maneesh Rao
Maneesh Rao

Reputation: 184

Query to get the details by matching date

Hi I am have converted the string to date format and trying to match by date and get the customer details but nothing is matching.

 $beginTimeStamp =  "1524853800";
 $endTimeStamp = "1525717800";
 $beginTime = date('Y-m-d', $beginTimeStamp);
 $beginTimeStamp = date('Y-m-d',  strtotime($beginTime . ' +1 day'));
 $endTime = date('Y-m-d', $endTimeStamp);
 $endTimeStamp = date('Y-m-d',  strtotime($endTime . ' +1 day'));

 $userDetails = DB::select('SELECT * from users where created_at >='.$beginTimeStamp.' AND created_at <= '.$endTimeStamp.'');

The date is coming by using below SQL query when I am using PHP variable.

Upvotes: 0

Views: 150

Answers (3)

apokryfos
apokryfos

Reputation: 40653

You are using Laravel so use the query builder and Carbon:

$beginTimeStamp =  "1524853800";
$endTimeStamp = "1525717800"; 
$userDetails = DB::table("users")
     ->where("created_at", ">=",  Carbon::createFromTimestamp($beginTimeStamp)->addDay())
     ->where("created_at", "<=",  Carbon::createFromTimestamp($endTimeStamp )->addDay())
     ->get();

Upvotes: 0

bharadwaja Gummadi
bharadwaja Gummadi

Reputation: 305

@Maneesh Rao,

Seem like few syntax errors in code.

  • $endTime = date('Y-m-d', $endTimeStamp) missing semi-colon.

  • 'SELECT * from users where created_at >='.$beginTimeStamp.' AND t1.created_at <= '.$endTimeStamp. -- Why t1?

Try this once

"SELECT * from users where created_at >='" . $beginTimeStamp."' AND created_at <='". $endTimeStamp."'";
    -

Upvotes: 0

pr1nc3
pr1nc3

Reputation: 8338

Your dates seem fine, they are in sql format.

The missing semicolon i guess it is just a "mistake" in copy paste otherwise you would get an error.

$userDetails = DB::select("SELECT * from users where created_at >={$beginTimeStamp} AND created_at <= {$endTimeStamp}");

Try this query. I think the error is coming from the way you try to concatenate the variables with the query and that you don't enclose your query with double quotes.

This query should also work:

$userDetails = DB::select("SELECT * from users where created_at >='$beginTimeStamp' AND t1.created_at <= '$endTimeStamp'");

The whole query is enclosed in double quotes and the variables inside single quotes.

Upvotes: 1

Related Questions