php 从指定的日期范围中获取记录
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 从指定的日期范围中获取记录相关的知识,希望对你有一定的参考价值。
https://carbon.nesbot.com/docs/
https://stackoverflow.com/questions/37153903/how-to-get-first-and-last-day-of-previous-month-with-carbon-laravel
https://stackoverflow.com/questions/33771643/get-records-from-database-base-on-the-specified-date-range-laravel-5
https://www.nzate.com/563/how-to-subtract-days-or-months-from-datetime-in-php
<?php
$current_sub = Subscription::where('user_id', '=',21)->get() ;
// dd( $current_sub[0]->stripe_plan ) ; // month_5
$sub_date = $current_sub[0]->created_at->toDateString() ;
echo 'Date of original Subscription: ' . $sub_date . '<br>' ;
$sub_date = explode('-', $sub_date) ;
// dd( $sub_date ) ; // array( 0 => 2018 , 1 => 04, 2 => 10)
// dd( $current_sub[0]->created_at->toDateTimeString() ) ; // 2018-04-10 22:37:39
// get current date time
$dt = \Carbon\Carbon::now() ;
$current_date = $dt->toDateString() ;
$parsed_current_date = \Carbon\Carbon::parse($dt)->toDateString() ;
echo 'Actual Current Date: ' . $parsed_current_date . '<br>' ;
$current_mod = explode('-', $current_date) ;
// swap is the date modified to match month and day of original subscription
//$swap = $dt->year($current_mod[0])->month( $current_mod[1] )->day( 04 )->toDateString() ;
$swap = $dt->year($current_mod[0])->month( $current_mod[1] )->day( $sub_date[2] )->toDateString() ;
// pass this to earlier, swap gets modified after addMonth()
// $modifiedswap = $dt->year($current_mod[0])->month( $current_mod[1] )->day( $sub_date[2] )->toDateString() ;
echo 'Modded date: ' . $swap . '<br>' ;
echo 'Current Date modified to match original sub date: ' . $swap . '<br>' ;
if ( \Carbon\Carbon::parse($current_date)->gt($swap ) ) {
$modified_date = \Carbon\Carbon::parse( $swap ) ;
echo 'Modified Date: ' . $modified_date . '<br>' ;
$end = $modified_date->addMonth() ; // ADD ONE MONTH FROM THIS DATE
$start = \Carbon\Carbon::parse( $swap ) ;
echo "<p style='color:red;'>Current Actual date is greater than Modded Date,
Now add one month to Modded Date: " . $start->toDateString(). '</p>' ;
}
else {
$modified_date = \Carbon\Carbon::parse( $swap ) ;
$start = $modified_date->subMonth() ; // SUBSTRACT ONE MONTH
$end = \Carbon\Carbon::parse( $swap ) ;
echo "<p style='color:green;'>Modded Date is greater than Current Actual Date,
Substract one month from Modded Date: " . $end->toDateString() . '</p>';
}
// dd( $swap ) ;
echo '<br>' ;
// get number of videos in this time range
//$earlier = \Carbon\Carbon::parse('2018-04-11');
//$earlier = \Carbon\Carbon::parse( $sub_date[0] . '-' . $sub_date[1] . '-' . $sub_date[2] );
//$earlier = \Carbon\Carbon::parse( $addonemonth );
//$later = \Carbon\Carbon::parse('2018-05-10');
//$later = \Carbon\Carbon::parse( $swap );
echo 'Getting Videos from: ' . $start . ' To ' . $end->toDateString() ;
//$videos = Video::whereBetween('created_at', [$earlier, $later])->get();
// for the inverse
$videos = Video::whereBetween('created_at', [$start, $end])->get();
dd($videos) ; // Records
dd($videos->count() ) ; // count these records
以上是关于php 从指定的日期范围中获取记录的主要内容,如果未能解决你的问题,请参考以下文章
如何从当前日期 PHP 获取最近 7 周、7 个月的日期范围?