Laravel add 30 minutes to created_add for expired - laravel

and I want to show all data in table Transaction using Laravel I'm using index.blade.php into Table
so, when someone makes a transaction, I give them 30 mins. example startTime= 2020-04-26 21:04:00endTime should be= 2020-04-26 21:34:00
so when current time= 2020-04-26 21:24:00, that row will change to yellow
whencurrent time= 2020-04-26 21:32:00````, that row will change to red
What should I do?
this is my index
this is my transaction.php
this is my transactionController#index
this is my transactionControlelr#store
this is my create table transaction

If you want to add 30 min to some time just do this:
[
startTime => date('Y-m-d H:i:s'),
endTime => date('Y-m-d H:i:s', strtotime('+30 minutes'))
]
If you need to add 30min to some specific datetime, then use this:
$specificDateTime = '2020-04-26 21:32:00';
$increased = date('Y-m-d H:i:s', strtotime($created_at . '+30 minutes'));

Related

Laravel Importing CSV throws error on rows with dates

Import.php
...
return new Statement([
'account_number' => $row['accountno'],
'account_name' => $row['name'],
'reading_date' => \Carbon\Carbon::createFromFormat('m/d/Y', $row['billdate']),
'due_date' => \Carbon\Carbon::createFromFormat('m/d/Y', $row['duedate']),
]);
...
Error:
Illuminate\Database\QueryException PHP 8.1.6 9.37.0
SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect date value: '10/18/2022' for column `mubsdb`.`statements`.`due_date` at row 1
INSERT INTO
`statements` (`due_date`, `reading_date`)
VALUES
( 10 / 18 / 2022, 10 / 03 / 2022),
(
10 / 18 / 2022,
10 / 03 / 2022
),
( 10 / 18 / 2022, 10 / 03 / 2022),
( 10 / 18 / 2022, 10 / 03 / 2022),
(10/18/2022, 10/03/2022), (10/18/2022, 10/03/2022), (10/18/2022, 10/03/2022),
DB Structure:
Name Type Null Default
reading_date date Yes NULL
due_date date Yes NULL
I'm trying to import and save csv rows to my DB but I get error with dates. I tried \Carbon\Carbon::createFromFormat('m/d/Y', $row['billdate']) and \Carbon\Carbon::parse($row['billdate'])->format('Y-m-d') but neither seems to work
weirdly, this worked.
'reading_date' => $row['billdate'] ? \Carbon\Carbon::createFromFormat('m/d/Y', $row['billdate'])->format('m/d/Y') : null,
'due_date' => $row['duedate'] ? \Carbon\Carbon::createFromFormat('m/d/Y', $row['duedate'])->format('m/d/Y') : null,
If you're using the newest version of laravel-excel, then you'll notice at this page a date column is exported using Date::dateTimeToExcel:
// ...
Date::dateTimeToExcel($invoice->created_at),
// ...
That is because date is stored as numbers in excel, thus a datetime object needs to be converted first in order to show the value correctly.
This rule also exists in import. So personally I would add a rule in the import class to make sure that the date we're receiving is a number (which actually a date format in excel):
use Maatwebsite\Excel\Concerns\WithValidation;
class MyImport implements WithValidation
{
public function rules(): array
{
return [
'created_at' => ['numeric'],
];
}
}
And then, when about to importing the data using model, convert the number to datetime before modifying it with Carbon:
use PhpOffice\PhpSpreadsheet\Shared\Date;
// ...
return new Import([
// ...
'created_at' => Carbon::instance(Date::excelToDateTimeObject($row['created_at'])),
// or any other Carbon methods
// ...
]);

carbon start of week sunday and endOfWeek Saturday not working

I need to select a particular users weekly top score. I am using Carbon and I forced Carbon startofWeek to SUNDAY and endOfWeek to SATURDAY.
Here is my DB data
id user_id score created_at updated_at
43 33 88 2020-02-23 00:00:00 NULL
44 33 15 2020-02-24 00:00:00 NULL
45 33 42 2020-02-24 00:00:00 NULL
46 33 86 2020-02-25 00:00:00 NULL
47 33 100 2020-02-04 00:00:00 NULL
Here is my code
$week = GameScore::select(max('score')
->where('created_at', '>=', Carbon::now()->startOfWeek(Carbon::SUNDAY))
->where('created_at', '<=', Carbon::now()->endOfWeek(Carbon::SATURDAY))
->where('user_id', $user->id)
->get();
According to my data I need to get score 88 as result , because this score is obtained on 23rd Feb Sunday which is this weeks starting and till today all other scores are not greater than this.
In your code you need to specify the date format when comparing,
Carbon::now()->endOfWeek(Carbon::SATURDAY)->format('Y-m-d H:i:s');
$en = CarbonImmutable::now()->locale('en_US');
$ar = CarbonImmutable::now()->locale('ar');
// We still can force to use an other day as start/end of week
$start = $en->startOfWeek(Carbon::TUESDAY);
$end = $en->endOfWeek(Carbon::SATURDAY);
var_dump($ar->endOfWeek()->format('Y-m-d H:i'));
Is working for me
You can set the starting day of the week by using the Carbon static function setWeekStartsAt and also can use that to work out the end of week day.
public static function getWeekStartAndEndDates($date, $startDayIndex = Carbon::SATURDAY)
{
$date = Carbon::parse($date);
$date::setWeekStartsAt($startDayIndex);
$from = $date->startOfWeek();
$date::setWeekEndsAt($from->copy()->addDays(6)->dayOfWeek);
$to = $date->copy()->endOfWeek();
return ['from' => $from, 'to' => $to];
}

I have 2 dates in Laravel, how can I run a auto generate post to go through between two dates with time period?

I want to create a Question (question table) that has startDate and endDate and period that is in hour (1-2-3-... hours) and user_id
Now I want to auto generate question rows in another table (Question_user) between two dates with period
ex:
startDate = 2019-11-01 00:00:00
endDate = 2019-11-20 12:00:00
period = 12
Now I want to create questions in (Question_user) at:
2019-11-01 00:00:00
2019-11-01 12:00:00
2019-11-02 00:00:00
...
How can I do this?
*Date format -->Year/Month/Day
in your controller , add below function :
protected function GenerateDateRange(Carbon $start_date, Carbon $end_date,$period)
{
$dates = [];
for($date = $start_date; $date->lte($end_date); $date->addHours($period)) {
$dates[] = $date->format('Y-m-d H:i:s');
}
return $dates;
}
and use it like this :
$dateRanges=$this->GenerateDateRange(Carbon::parse("2019-11-01 00:00:00"),Carbon::parse("2019-11-20 12:00:00"),12);
then for loop through $dateRanges and insert your data .

Rx extension - creating minute bar from sequence

I have a sequence where values are generated at random times (real time stock market prices). I have a requirement to find the highest and lowest value of the sequence between a one minute period. I know you can use something like Buffer for this.
But the minute window should start with 00 seconds and finish at 59 seconds. e.g. the minute should start from 8:00:00 and finish at 8:00:59 the second minute should start from 8:01:00 to 8:01:59. Can we do this with Rx? Thanks. Vipter
I believe this would work:
var query =
source
.Publish(ss =>
ss
.GroupByUntil(
x => x.Timestamp.ToUnixTimeSeconds() / 60,
x => x.Value,
x => ss.Where(s => x.Key != s.Timestamp.ToUnixTimeSeconds() / 60))
.Select(gxs => gxs.ToArray().Select(xs => new
{
min = xs.Min(), max = xs.Max()
})));

Laravel 5 and Carbon discrepancy on Forge

Hopefully I'm not mad and I'm only missing something. I have a project on Laravel 5.0 and I have a requestExpired function called every time I have an incoming request. Now, to calculate the difference between current time on the server and the timestamp within the request I'm using:
$now = Carbon::now('UTC');
$postedTime = Carbon::createFromTimestamp($timestamp, 'UTC');
For some reason request is always rejected because it's expired. When I debug these two lines from above and just dump data, I get:
REQUEST'S TIMESTAMP IS: 1423830908279
$NOW OBJECT: Carbon\Carbon Object
(
[date] => 2015-02-13 12:35:08.000000
[timezone_type] => 3
[timezone] => UTC
)
$POSTEDTIME OBJECT: Carbon\Carbon Object
(
[date] => 47089-05-28 09:37:59.000000
[timezone_type] => 3
[timezone] => UTC
)
Any ideas why $postedTime is so wrong? Thanks!
To answer my own question: for some strange reason webhook calls from remote API have 13 digits long timestamps and that's why my dates were so wrong.

Resources