1

i find data between date current from database has start and end date from 2 attribute

this is database

and this is sql code

select * from period WHERE
priceId = 1788749 AND
'2018-05-07 02:00:00' BETWEEN startPeriod AND endPeriod

but,i don't know how i can use in laravel

i'm try

$now = Carbon::now()->toDateTimeString();
$prod =DB::table('period')->where('priceId','=', 1788749)
->wherebetween($now, ['startPeriod', 'endPeriod'])
->get();

2 Answers 2

1

Read the documentation here https://laravel.com/docs/5.6/eloquent

Almost laravel application use eloquent as their sql package. The one you use is a DB facade which is okay also.

if translate to eloquent your query will be:

$prod = Period::where('priceId',1788749)->where('startPeriod','>',$now)->where('endPeriod','<',$now)->get()

startPeriod and endPeriod should date when you migrate it in the database

i dont think you can use whereBetween because it required as a first params is the column name of the table not a dynamic value

Sign up to request clarification or add additional context in comments.

1 Comment

nice , but > switch < ( 'startPeriod','>',$now )
1

You can take a look at the carbon's diffInDays/diffInMonths/diffInYears (according to your requirements) function for getting the difference between two dates.

Make sure to parse the date according to the carbon format using this. $convertedDate = Carbon::parse($yourDate);

http://carbon.nesbot.com/docs/

Happy Coding

Comments

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.