How to use a variable inside activerecord query in yii2 - activerecord

How do I convert the following to active record style in yii2?
SELECT *
FROM orders
WHERE created_at >= (CURDATE() - INTERVAL 11 MONTH)
ORDER BY id DESC
What I have tried:
switch ($period) {
case 'y':
$p = "12 month";
break;
case 'm':
$p = "1 month";
break;
case 'w':
$p = "7 days";
break;
case 'd':
$p = "1 days";
break;
}
Customers::find()
->where('>=', 'created_at', (CURDATE() - INTERVAL $p))
->orderBy('id DESC');
But I get:
syntax error, unexpected '$p' (T_VARIABLE)

insted of pass the string
you could pass the $p as a param
Customers::find()
->where( 'created_at >=(CURDATE() - INTERVAL :p' ), [':p'=>$p])
->orderBy('id DESC');

Yii2 has an Expression class to help with those kinds of things (doc here).
In your case, it'd look something like:
Customers::find()
->where(['>=', 'created_at', new \yii\db\Expression('(CURDATE() - INTERVAL ' . $p . ')'))
->orderBy('id DESC');

Related

How to get missing dates between 7 latest rows from database?

I want to get 7 latest rows (in order from down to up), for the current week (Sunday to Saturday), for the current logged in user.
To do this, I used this one method:
Carbon::setWeekStartsAt(Carbon::SUNDAY);
Carbon::setWeekEndsAt(Carbon::SATURDAY);
$strikes = UserStrike::where('user_id', $user)->whereBetween('created_at', [Carbon::now()->startOfWeek(), Carbon::now()->endOfWeek()])->latest()->take(7)->get();
$strikes = $strikes->reverse(); //to order the list from DESC to ASC
But the problem with this method is that it doesn't get any missing days.
So if there are data like this for the current week (2020-05-12 is missing):
created_at: 2020-05-10
created_at: 2020-05-11
created_at: 2020-05-13
Then for that one day which is missing, there should be a null in array. Something like this:
$days = ["Sun", "Mon", null, "Wed"];
I'm having hard time to find a way to replace missing day with a null in array.
If anyone here can help me with this problem please leave your comment. Thank you in advance.
You can take data from DB and then create array by iterating on DateTimeInterval(). I don't know Laravel and Carbon well but it could be something like this:
Carbon::setWeekStartsAt(Carbon::SUNDAY);
Carbon::setWeekEndsAt(Carbon::SATURDAY);
$start = Carbon::now()->startOfWeek();
$end = Carbon::now()->endOfWeek();
$strikes = UserStrike::where('user_id', $user)
->whereBetween(
'created_at',
[$start, $end]
)
->latest()
->take(7)
->get();
$strikes = $strikes->reverse();
$timePeriod = new DatePeriod(
$start,
new DateInterval('P1D'),
$end
);
$days = [];
for ($i = 0; $i < count($timePeriod) $i++) {
$days[$i] = null;
foreach ($strikes as $strike) {
if ($strike->created_at->format('Y-m-d') == $timePeriod[$i]->format('Y-m-d') {
$days[$i] = $strike->created_at->format('Y-m-d');
break;
}
}
}

How to fetch users who does not have reports for 6 consecutive months using laravel?

I'm developing a report system, and I want to fetch users who did not report for six (6) consecutive months. How do I achieve this?
I've tried the code below but I'm not getting the desired output. There is also a problem. Let's say the date interval is 12 months. How can I determine if there is no report for 6 consecutive months?
$dateStart = '2018-10-31';
$dateEnd = '2019-03-31';
$intervals = Carbon::parse($dateStart)->diffInMonths($dateEnd);
$users = $users->whereDoesntHave('reports', function($query) use($intervals) {
for ($i = 5; $i >= 0; $i--) {
$firstMonth = Carbon::parse($dateEnd)->subMonthsNoOverflow($intervals);
$query->where('date', '>=', $firstMonth->format('Y-m-d'))->where('date', '<=', $dateEnd);
}
});
What I will do is that I will create a loop per month based on the start and end date, then check if he did not have a report for that month. If it doesn't have a report for that month, I will increment a counter, and if that counter reaches 6 counts, exit the loop and the condition was satisfied.
Below is the basic idea:
$dateStart = '2018-10-31';
$dateEnd = '2019-10-31';
$count = 0;
$no_report_for_6_consecutive_months = 0 ;
startloop
$have_report = Model::whereMonth('date_column', $date_of_loop->format('m'))->get();
if($have_report->count()){
$count = 0;
}
else{
$count++;
}
if($count==6){
$no_report_for_6_consecutive_months = 1 ;
break;
}
endloop
You have to find distinct users who report for six (6) consecutive and get difference with all users.
$enddate = 2019-04-15;
$startdate = date("Y-m-d", strtotime("-6 months", strtotime($enddate)));
$users = User::all();
$usersIdArray = $users->pluck("id")->all();
$reportedBy = Report::where('date', '>=', $startdate)
->where('date', '<=', $enddate)
->distinct("user_id")->get();
$reportedByIdArray = $reportedBy ->pluck("id")->all();
$notReportedByIdArray = array_values(array_diff($usersIdArray , $reportedByIdArray));
$notREportedUsers = User::whereIn(id", $notReportedByIdArray)->get();
//its a way but not tested
I have modified the query from this answer
I believe the query can be wrote cleaner. I will let you do this if you want to.
App\User::select('*')
->from(\DB::raw("(select
low.*,
low.`date` as date_start,
high.`date` as date_end,
to_days(high.`date`) - to_days(low.`date`) as day_gap,
period_diff(date_format(high.`date`, '%Y%m'),
date_format(low.`date`, '%Y%m')) as month_gap
from reports low, reports high
where high.`date` =
(select
min(`date`)
from reports
where
`date` > low.`date`
and low.user_id = high.user_id
)
) as d")
)->get();
Now you will get all the users with 4 extra fields: date_start; date_end, day_gap and month_gap
If you want the users with a month gap of 6 months you can do this:
App\User::select('*')
->from(\DB::raw("..."))
->where('month_gap', '>=', 6)
->get();

Timepicker that removes times as they're selected (ajax)

I'm building a booking form for a moving business that uses a calendar combined with a start and end time. I built the timepicker with Formidable Pro, and it allows me to check "unique" on time fields which automatically removes them on the selected date. However it doesn't automatically remove the times from within the range between start and end times (ie: if someone chooses to rent a truck from 1am-3am I need 1am,2am,and 3am to be removed from future options but right now it only removes 1am and 3am) . I need to write ajax to remove the in-between times from the options. I'm not sure where to begin. This is the current ajax_time_ options function. Any push in the right direction would be appreciated.
function ajax_time_options(){
global $frmpro_settings, $frmdb, $wpdb;
//posted vars = $time_field, $date_field, $step, $start, $end, $date, $clock
extract($_POST);
$time_key = str_replace('field_', '', $time_field);
$date_key = str_replace('field_', '', $date_field);
if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', trim($date)))
$date = FrmProAppHelper::convert_date($date, $frmpro_settings->date_format, 'Y-m-d');
$date_entries = FrmEntryMeta::getEntryIds("fi.field_key='$date_key' and meta_value='$date'");
$opts = array('' => '');
$time = strtotime($start);
$end = strtotime($end);
$step = explode(':', $step);
$step = (isset($step[1])) ? ($step[0] * 3600 + $step[1] * 60) : ($step[0] * 60);
$format = ($clock) ? 'H:i' : 'h:i A';
while($time <= $end){
$opts[date($format, $time)] = date($format, $time);
$time += $step;
}
if($date_entries and !empty($date_entries)){
$used_times = $wpdb->get_col("SELECT meta_value FROM $frmdb->entry_metas it LEFT JOIN $frmdb->fields fi ON (it.field_id = fi.id) WHERE fi.field_key='$time_key' and it.item_id in (". implode(',', $date_entries).")");
if($used_times and !empty($used_times)){
$number_allowed = apply_filters('frm_allowed_time_count', 1, $time_key, $date_key);
$count = array();
foreach($used_times as $used){
if(!isset($opts[$used]))
continue;
if(!isset($count[$used]))
$count[$used] = 0;
$count[$used]++;
if((int)$count[$used] >= $number_allowed)
unset($opts[$used]);
}
unset($count);
}
}
echo json_encode($opts);
die();
}

Display "time ago" instead of datetime in PHP Codeigniter

I would like to display a time format like twitter and FB (Posted 3 hours ago, Posted 2 minutes ago and so on...)
I've tried this piece of code without success :
function format_interval($timestamp, $granularity = 2) {
$units = array('1 year|#count years' => 31536000, '1 week|#count weeks' => 604800, '1 day|#count days' => 86400, '1 hour|#count hours' => 3600, '1 min|#count min' => 60, '1 sec|#count sec' => 1);
$output = '';
foreach ($units as $key => $value) {
$key = explode('|', $key);
if ($timestamp >= $value) {
$floor = floor($timestamp / $value);
$output .= ($output ? ' ' : '') . ($floor == 1 ? $key[0] : str_replace('#count', $floor, $key[1]));
$timestamp %= $value;
$granularity--;
}
if ($granularity == 0) {
break;
}
}
I use this function with a callback into another function like : $this->format_interval(); and pass it to my View
My current format date is : 2012-07-26 09:31:pm and already stored in my DB
Any help will be very appreciated!
The Date Helper's timespan() method just does that:
The most common purpose for this function is to show how much time has elapsed from some point in time in the past to now.
Given a timestamp, it will show how much time has elapsed in this format:
1 Year, 10 Months, 2 Weeks, 5 Days, 10 Hours, 16 Minutes
So, in your example, all you need to do is convert your date to a timestamp and do something like this:
$post_date = '13436714242';
$now = time();
// will echo "2 hours ago" (at the time of this post)
echo timespan($post_date, $now) . ' ago';
Try something like this in a my_date_helper.php file (source: Codeigniter Forums):
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
if( ! function_exists('relative_time'))
{
function relative_time($datetime)
{
$CI =& get_instance();
$CI->lang->load('date');
if(!is_numeric($datetime))
{
$val = explode(" ",$datetime);
$date = explode("-",$val[0]);
$time = explode(":",$val[1]);
$datetime = mktime($time[0],$time[1],$time[2],$date[1],$date[2],$date[0]);
}
$difference = time() - $datetime;
$periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
$lengths = array("60","60","24","7","4.35","12","10");
if ($difference > 0)
{
$ending = $CI->lang->line('date_ago');
}
else
{
$difference = -$difference;
$ending = $CI->lang->line('date_to_go');
}
for($j = 0; $difference >= $lengths[$j]; $j++)
{
$difference /= $lengths[$j];
}
$difference = round($difference);
if($difference != 1)
{
$period = strtolower($CI->lang->line('date_'.$periods[$j].'s'));
} else {
$period = strtolower($CI->lang->line('date_'.$periods[$j]));
}
return "$difference $period $ending";
}
}
The format is a little different than the one you're using in your database (why do you mark times with pm/am rather than just use 24 hour times and convert for the frontend?). Either way, shouldn't take much work to get it working.
I had a function that solved this like this:
$int_diff = (time() - $int_time);
$str_this_year = date('Y-01-01', $int_time);
$str_weekday = t('time_weekday_'.strtolower(date('l', $int_time)));
$str_month = t('time_month_'.strtolower(date('F', $int_time)));
$arr_time_formats = array( '-90 seconds' => t('time_a_minute_at_most'),
'-45 minutes' => t('time_minutes_ago', ceil($int_diff / (60))),
'-70 minutes' => t('time_an_hour_at_most'),
'-8 hours' => t('time_hours_ago', ceil($int_diff / (60 * 60))),
'today' => t('time_hours_ago', ceil($int_diff / (60 * 60))),
'yesterday' => t('time_yesterday', date('H:i', $int_time)),
'-4 days' => t('time_week_ago', $str_weekday, date('H:i', $int_time)),
$str_this_year => t('time_date', date('j', $int_time), $str_month, date('H:i', $int_time)),
0 => t('time_date_year', date('j', $int_time), $str_month, date('Y', $int_time), date('H:i', $int_time)));
if ($boo_whole)
return $arr_time_formats[0];
foreach(array_keys($arr_time_formats) as $h)
if ($int_time >= strtotime($h))
return $arr_time_formats[$h];
Basicly t() is a function combined with $this->lang->line() and sprintf(). The idea here is to give keys that's runned through strtotime() till you reach the closest time, with 0 being the fallback.
This approach is really good since you can easy adjust the times with a nice overview. I could give more piece of the code, but it feels like doing too much of the work :) Basicly this is just the theory behind how you can do it.
<?php
$this->load->helper('date');
//client created date get from database
$date=$client_list->created_date;
// Declare timestamps
$last = new DateTime($date);
$now = new DateTime( date( 'Y-m-d h:i:s', time() )) ;
// Find difference
$interval = $last->diff($now);
// Store in variable to be used for calculation etc
$years = (int)$interval->format('%Y');
$months = (int)$interval->format('%m');
$days = (int)$interval->format('%d');
$hours = (int)$interval->format('%H');
$minutes = (int)$interval->format('%i');
// $now = date('Y-m-d H:i:s');
if($years > 0)
{
echo $years.' Years '.$months.' Months '.$days.' Days '. $hours.' Hours '.$minutes.' minutes ago.' ;
}
else if($months > 0)
{
echo $months.' Months '.$days.' Days '. $hours.' Hours '.$minutes.' minutes ago.' ;
}
else if($days > 0)
{
echo $days.' Days '.$hours.' Hours '.$minutes.' minutes ago.' ;
}
else if($hours > 0)
{
echo $hours.' Hours '.$minutes.' minutes ago.' ;
}
else
{
echo $minutes.' minutes ago.' ;
}
?>

How to iterate through range of Dates?

In my script, I need to iterate through a range of dates given the start date and end date. How can I do this in Perl?
Use DateTime module. Here is a simple example which lists the ten previous days:
use 5.012;
use warnings;
use DateTime;
my $end = DateTime->now;
my $day = $end->clone->subtract( days => 10 ); # ten days ago
while ($day < $end) {
say $day;
$day->add( days => 1 ); # move along to next day
}
Update (after seeing your comment/update):
To parse in a date string then look at the DateTime::Format on modules CPAN.
Here is an example using DateTime::Format::DateParse which does parse YYYY/MM/DD:
use DateTime::Format::DateParse;
my $d = DateTime::Format::DateParse->parse_datetime( '2010/06/23' );
One easy approach is to use the Date::Simple module, which makes use of operator-overloading:
use strict;
use warnings;
use Date::Simple;
my $date = Date::Simple->new ( '2010-01-01' ); # Stores Date::Simple object
my $endDate = Date::Simple->today; # Today's date
while ( ++$date < $endDate ) {
print ( $date - $endDate ) , "day",
( ( $date-$endDate) == 1 ? '' : 's' ), " ago\n";
}
use DateTime::Format::Strptime qw();
my $start = DateTime::Format::Strptime->new(pattern => '%Y/%m/%d')->parse_datetime('2010/08/16');
my $end = DateTime::Format::Strptime->new(pattern => '%Y/%m/%d')->parse_datetime('2010/11/24');
while ($start < $end) {
$start->add(days => 1);
say $start->ymd('/');
}
I like to use the fact that strftime will normalize the date for me:
#!/usr/bin/perl
use strict;
use warnings;
use POSIX qw/strftime/;
my $start = "2010/08/16";
my $end = "2010/09/16";
my #time = (0, 0, 0);
my ($y, $m, $d) = split "/", $start;
$y -= 1900;
$m--;
my $offset = 0;
while ((my $date = strftime "%Y/%m/%d", #time, $d + $offset, $m, $y) le $end) {
print "$date\n";
} continue {
$offset++;
}
You can try Date::Calc::Iterator
# This puts all the dates from Dec 1, 2003 to Dec 10, 2003 in #dates1
# #dates1 will contain ([2003,12,1],[2003,12,2] ... [2003,12,10]) ;
my $i1 = Date::Calc::Iterator->new(from => [2003,12,1], to => [2003,12,10]) ;
my #dates1 ;
push #dates1,$_ while $_ = $i1->next ;
If installing extra perl modules is not preferable, one can use this approach, based on a core perl library POSIX:
#!/usr/bin/perl
use strict;
use warnings;
use POSIX qw(strftime);
# CREATE CALENDAR
my #Calendar = ();
my $years = 3;
my #Now = localtime(); # An array of 9 date-time parameters.
for my $count ( 0 .. ( 365 * $years ) ) {
# If date is January 1st, manual shift to December 31st is needed,
# because days ([yday][2]) are counted from January 31st and never shift back one year.
if( $Now[4] == 0 && $Now[3] == 1 ) {
unshift #Calendar, strftime( "%Y-%m-%d", #Now );
$Now[5] --; # Reduce by one the sixth array element #5 - year.
$Now[4] = 11; # Set fifth array element № 4 - to December.
$Now[3] = 31; # Set fourth array element № 3 - to 31st.
} else {
unshift #Calendar, strftime( "%Y-%m-%d", #Now );
$Now[3] --;
}
}
# Print out.
my $size = #Calendar;
for (my $i = 0; $i < $size; $i++) {
print $Calendar[$i]."\n";
}
Perl has a rich array of time and date manipulation modules, as seen here:
http://datetime.perl.org/?Modules
And there are some examples of date and time problems there as well.
With Perl, there's always more than one way to do it.

Resources