How to filter timestamp of laravel? - php

I want to filter the result of the records within the period, but i can't find any example of how to use the where clause to get the exact range of data.
These are the data posted from form:
[monthRangeStart] => 12
[yearRangeStart] => 2013
[monthRangeEnd] => 12
[yearRangeEnd] => 2013
and the following is the format of updated_at timestamp
2013-12-18 07:22:34
How should i finish the line of code
$trans = Transaction::where('updated_at', )

In my opinion if you decided to use some framework you should use tools it provides in order to create better solutions. Laravel has very beautiful Carbon class for date manipulation. This is solution I came to:
$trans = Transaction::whereBetween('updated_at', [
\Carbon\Carbon::createFromDate(Input::get('yearRangeStart'), Input::get('monthRangeStart'))->startOfMonth(),
\Carbon\Carbon::createFromDate(Input::get('yearRangeEnd'), Input::get('monthRangeEnd'))->endOfMonth()
])->get();
In this case you will never go wrong with number of days in any specific month.

You may try something like this
$start = Input::get('yearRangeStart') . '-' . Input::get('monthRangeStart');
$end = Input::get('yearRangeEnd') . '-' . Input::get('monthRangeEnd') . '-' . '31';
$trans = Transaction::whereBetween('updated_at', array($start, $end))->get();
Update :
$start = Input::get('yearRangeStart') . '-' . Input::get('monthRangeStart');
$endYear = Input::get('yearRangeEnd');
$endMonth = Input::get('monthRangeEnd');
$endDays = cal_days_in_month(CAL_GREGORIAN, $endMonth, $endYear);
$end = $endYear . '-' . $endMonth . '-' . $endDays;
$trans = Transaction::whereBetween('updated_at', array($start, $end))->get();

This should do it...
$trans = Transaction::where('updated_at', '>', "$yearRangeStart-$monthRangeStart-00 00:00:00")
->where('updated_at', '<', "$yearRangeEnd-$monthRangeEnd-31 99:99:99")
->get();
If you decide you need to filter on the days/hours/minutes/seconds as well, you can replace those as you go.

Related

Reset automatic code number every year on laravel

guys! i have automatic code number for invoice. the automatic code number is "xx/month/year". i want "xx" in my automatic code number reset to 1 when the year change or every 1 january. this is my code
function noinvoice()
{
$latest = Pengiriman::latest()->first();
$month = date('m');
$year = date('Y');
if (!$latest) {
return '1/' . $month . '/' . $year;
}
$string = preg_replace("/[^0-9\.]/", '', $latest->noinvoice);
return sprintf($string + 1) . '/' . $month . '/' . $year;
}
You need to inspect your $latest object. If $latest doesn't exist OR its year doesn't match this year, then the invoice number is 1. Otherwise, it's $latest->noinvoice + 1.
You never mentioned where you store your invoice creation date, so I'll assume you use the default created_at Laravel approach. For this task you should add one more condition to your if statement:
if (!$latest or date('Y', strtotime($latest->created_at)) != $year) {

Laravel date range not working with eloquent

Hi I have one column name as date which will hold advance payment date. I want to fetch current month date advance paid. for what i am using below code.
echo $startDate = date($currentYear . "-" . $currentMonth . "-1");
echo $endDate = date($currentYear . "-" . $currentMonth . "-" . $numberOfDaysInMonth);
DB::enableQueryLog();
echo $advances = UserServiceAdvancePayment::where('user_service_master_id', $userServiceMasterObject->id)
->whereDate('date', ">=", $startDate)
->whereDate('date', "<", $endDate)
->get();
Herein am getting data when am applying one date condition but when applying other it is not returning data. Also tried with between there also same issue. Any help is deeply appreciated on this
If I understand what you need to do correctly then it's easier to use Carbon to do it:
$startDate = CarbonImmutable::createFromDate($currentYear, $currentMonth, 1)
$endDate = CarbonImmutable::createFromDate($currentYear, $currentMonth, 1)->endOfMonth();
DB::enableQueryLog();
$advances = UserServiceAdvancePayment::where('user_service_master_id', $userServiceMasterObject->id)
->whereDate('date', ">=", $startDate)
->whereDate('date', "<=", $endDate)
->get();

How to set dynamically a dateperiod/daterange in function of a given date?

I'm searching the way to write a function to set a date period / date interval with a given date....
First, let me explain the context :
I have an application to manage wedding inscriptions.
I use for my archives page a system with "seasonnal dates" like
2017-2018
2018-2019
2019-2020 etc.
Each season (or date period) starts the 1st september and finishes the 31 august, so for example 2017-2018 correspond to 2017-09-01 to 2018-08-31, 2018-2019 correspond to 2018-09-01 to 2019-08-31 etc.
So if I have a wedding date like 2019-01-23 I want to save the season corresponding, right here 2018-2019.
Actually I write this code :
//for example (I have a date object in fact in this $evenement var)
$evenement = "2019-01-23";
$start = Carbon::createFromFormat('Y-m-d', $evenement->copy()->subYear()->year . '09-01');
$end = Carbon::createFromFormat('Y-m-d', $evenement->year . '08-31');
if($evenement->between($depart, $fin)){
$saison = $depart->year . '-' . $fin->year;
}
If the date is after 2019-01-01, it works but not for before and not for the future date, (and not a function at this time)...
Can you help me to achieve this ? It will be very nice.
Thanks
I found the solution during the last night !
This is my function to get the "season" in function of the given date.
public static function getSeason($date)
{
if($date->month >= 9 && $date->month <= 12){
$annee1 = $date->year;
$annee2 = $annee1 + 1;
$saison = $annee1 . '-' . $annee2;
}
elseif($date->month >= 1 && $date->month <= 8){
$annee2 = $date->year;
$annee1 = $annee2 - 1;
$saison = $annee1 . '-' . $annee2;
}
return $saison;
}
So for example, if the date is 2018-10-03,
$season = "2018-2019";
And if date = 2019-04-21,
$season = "2018-2019";
What do you think about this function, can I optimize it ?
Thank you

Filter based on days of the week in a PHP foreach

I am getting from Facebook Graph API the last 3 months page views information from all the accounts that I am admin with the following method:
$since_date = date('Y-m-d', strtotime('-3 months'));
$until_date = date('Y-m-d');
$page_views = $facebook->api('/' . $account['id'] . '/insights/page_views?since=' . $since_date . '&until=' . $until_date . '','GET');
After that I am using a php foreach loop (actually two) to get the desired information (page views and the date) in a required format (an array) like this:
foreach ($page_views['data'] as $page_view) {
$i = 0;
$len = count($page_view['values']);
foreach($page_view['values'] as $key => $page_view_values) {
$end_time = strval($page_view_values['end_time']);
$value = intval($page_view_values['value']);
echo '[\'' . substr($end_time, 0, -14) . '\', ' . $value . ']';
if ($i == $len - 1) {
continue;
} else {
echo ', ';
}
$i++;
}
Which output for further needs the following array:
(.....],['2013-04-02', 21], ['2013-04-03', 7], ['2013-04-04', 2], ['2013-04-05', 0], ['2013-04-06', 2], ['2013-04-07', 3], ['2013-04-08', 1], ['2013-04-09', 2], ['2013-04-10', 5], ['2013-04-11', 1], ['2013-04-12', 11], ['2013-04-13', 0],[.....)
All is working like it should, with the above array. I render a very nice chart but I want to filter it if I could based on days of the week. For exemple: I want an array like the one above with only the dates from Monday or Tuesday and so on... I need to filter somewhere in the second php foreach to return only the days that I want but how? What condition is used for this kind of needs? Or I should use other method, for example, query something else from the Facebook Graph Api? Any guidance is more than welcomed.
You could do for example a switch case based on the 3-letter code of the week day, and do something like this for each day:
$tempDate = '2012-07-10';
echo date('D', strtotime( $tempDate));
It will output: Tue
This will allow you to filter by the week day. For example, you would only add to the array if the weekday code is Tue. All others will be skipped.
Added #arraintxo note:
date('N', strtotime($tempDate))
would return a numeric value of the week day (1 to 7, 1 being Monday and 7 Sunday), easier to compare.
I'd suggest something similar to the current reply, with slight adjustments:
$first=false; $cache=array(); $conv=new \Date('2000-01-01T00:00:00Z');
foreach ($page_views['data'] as $page_view) {
foreach($page_view['values'] as $key => $page_view_values) {
$end_time = strval($page_view_values['end_time']);
$ds=substr($end_time, 0, -14);
$value = intval($page_view_values['value']);
if(!isset($cache[$ds])
$cache[$ds]=(int)$conv->modify($ds)->format('N');
if($first){ echo ', '; $first=false; }
echo '[\'' . $ds . '\', ' . $value . ', '.$cache[$ds].']';
}
}
The day indices are now present as the third items of the arrays. Notice 1: a cache is used so that you look up every date only once. Notice 2: using a bool to not add leading separators is much cheaper than counting down from a count which is in turn cheaper than counting up to a count (what you did in the first place). Notice 3: This modifies the $conv object again and again, because I don't like creating many objects, and this ought to perform at least as well as the procedural style (date() and strtotime()).
In the first place I want to thank you all for the prompt replies, it was a great help received and I am very grateful for it. Based on your guidelines I solve my "issue" in the following manner:
$since_date = date('Y-m-d', strtotime('-3 months'));
$until_date = date('Y-m-d');
$page_views = $facebook->api('/' . $account['id'] . '/insights/page_views?since=' . $since_date . '&until=' . $until_date . '','GET');
foreach ($page_views['data'] as $page_view) {
$i = 0;
foreach($page_view['values'] as $key => $page_view_values) {
$end_time = strval($page_view_values['end_time']);
$ds = substr($end_time, 0, -14);
$value = intval($page_view_values['value']);
if (date('N', strtotime($ds)) != 1) {
continue;
} else {
if ($i==1) {
echo ', ';
}
$i=1;
echo '[\'' . $ds . '\', ' . $value . ']';
}
}
}
I can do this for every day of the week by using date('N', strtotime($ds)) for the corresponding number of the day. I receive the array that I want but if I can, i want to know if is possible to sum the numbers in the variables called $page_view_values['value'] (i want to make a comparison between the days in the week).

Efficient way to parse "ddmmyyyyThhmm" string to workable date string

I have the following types of strings:
17082011T1015
In other words: day month year T hours minutes.
What would be an efficient way to parse these strings in such a way that I end with something like a MySQL-like date:
`2011-08-17 10:15`
(There's no MySQL involved here by the way)
It's for Windows PHP < 5.3 so strptime() and date_parse_from_format()/DateTime::createFromFormat() are NOT an option.
It's just rearranging a string.
$date = "17082011T1015";
$day = substr($date, 0, 2);
$month = substr($date, 2, 2);
$year = substr($date, 4, 4);
$hour = substr($date, 9, 2);
$minute = substr($date, 11, 2);
echo $year . '-' . $month . '-' . $day . ' ' . $hour . ':' . $minute;
I think I'll end up using this solution:
sscanf( '17082011T1015', '%02s%02s%04sT%02s%02s', $day, $month, $year, $hours, $minutes );
$datetime = $year . '-' . $month . '-' . $day . ' ' . $hours . ':' . $minutes;
Before asking the question I didn't realize I could utilize the optional arguments to sscanf() to my advantage, and thought I would have to resort to something like this:
$parsed = sscanf( '17082011T1015', '%02s%02s%04sT%02s%02s' );
$datetime = $parsed[ 2 ] . '-' . $parsed[ 1 ] . '-' . $parsed[ 0 ] . ' ' . $parsed[ 3 ] . ':' . $parsed[ 4 ];
Not too bad either, but I like to be pretty clear as possible with named variables, so that it's obvious what this little routine does.
If you are using PHP >= 5.3.0, you may use DateTime::createFromTimeFormat to create ordinary DateTime object from that string.
Should be something like:
$str = "17082011T1015";
$date = DateTime::createFromFormat($str,'DDMMYYYYThhII');
echo $date->format("Y-m-d H:i:s");
Can't test it here, only have PHP 5.2. But I think you could figure the right format yourself.

Categories