I have a form and before saving it in the DB is it possible to make a calculation?
I get a date of birth and want to make an account with the current year.
Example: 10-10-1990 and I want it to subtract from the current year.
1990 - 2021
$tabel->datenasc = $request->datenasc;
You can still do your calculations before inserting into DB
// if $request->datenasc == 10-10-1990
$current_year = date("Y");
list($day, $month, $year) = explode("-", $request->datenasc);
$year_diff = $current_year - $year;
you can now insert $year_diff in the database;
You can use setYear method from carbon library
$date = Carbon::parse("10-10-1990");
$dateWithCurrentYear = $date->setYear(now()->format('Y'));
Related
i want to sum amount logs created today in my Repeat table in database i tried followings but not working
$start = (new Carbon('now'))->hour(0)->minute(0)->second(0);
$end = (new Carbon('now'))->hour(23)->minute(59)->second(59);
$data['daily'] = Repeat::where('user_id',Auth::user()->id)->where('created_at',[$start , $end])->sum('amount');
also tried
$start = carbon::today();
$data['daily'] = Repeat::where('user_id',Auth::user()->id)->where('created_at',$start)->sum('amount');
If you want to get today's record by using carbon you can do it as below.
$data['daily'] = Repeat::where('user_id',Auth::user()->id)->whereDate('created_at', Carbon::today())->sum('amount');
you can use Carbon::now() or Carbon::today() but check this amount should be integer or float if it string then sum will be 0(zero)
$data['daily'] = Repeat::where('user_id',Auth::user()->id)->whereDate('created_at', Carbon::now())->sum('amount');```
I have tried to access date stored in my db table and compare it with current date so that I can get the number of days but it shows this error
DateTime::__construct(): Failed to parse time string ([{"quit_date":null},{"quit_date":null}]) at position 0 ([): Unexpected character
This is the code that use in my controller
$quit_date = Information::select('quit_date')
->where('user_id','=',\Auth::user()->id)
->get();
$date = new Carbon($quit_date);
$now = Carbon::now();
$day = $date->diffInDays($now);
but if I set the $quit_date manually with the date for example "2019-04-25 00:00:00.000000", the code works fine and shows the days different between the dates, but when I use the Information::select to read the date from database, it shows error.
use Auth; //top of controller
$checkdate = Information::
->where('user_id','=',Auth::user()->id)
->first();
$quit_date=$checkdate->quit_date;
$date = new Carbon($quit_date);
$now = Carbon::now();
$day = $date->diffInDays($now);
The issue occurs because you are using ->get() at the end of your query. That method returns a Collection not a single object. The issue is solved by using ->first() to return a single object.
The error itself is because in the line $date = new Carbon($quit_date);, Carbon cannot convert a Collection to a date.
This should work:
$quit_date = Information::select('quit_date')
->where('user_id','=', \Auth::user()->id)
->first(); //Changed this from ->get()
$date = new Carbon($quit_date);
$now = Carbon::now();
$day = $date->diffInDays($now);
On my model I am trying to make a function so when I reload my page it will check if any rows have a code set and check the date_modified time 2017-02-16 13:53:18 and if has been present for more than 15min will remove row.
Question: On each post The column "code" if has any value then will check the date and time if has been there longer 15 min with code value still present then will remove row.
I have tried this below but when I reload page it does not remove any row.
Model Function
public function clear_unconfirmed_post() {
$this->db->where('code is NOT NULL', NULL, FALSE);
$this->db->where('date_modified < UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 15 MINUTE))', NULL, FALSE);
$this->db->delete($this->db->dbprefix . 'post');
}
Controller Construct Area
public function __construct() {
parent::__construct();
$this->load->model('catalog/forum/newthread_model');
$this->newthread_model->clear_unconfirmed_post();
}
Your date_modified field is of type DATETIME and you are comparing it with a integer (that is was UNIX_TIMESTAMP function returns).
I would advise as a general rule of thumb, using UNIX timestamp (i.e - INT fields) directly when storing date-time values, for easier comparisons and for not being affected by timezones, as UNIX timestamps are in UTC (+0), so they can be used very easily for timezones conversions.
So when storing a value in the date_modified field, instead of storing a value like this (the example shows variable assignment in PHP):
$date_modified = date("Y-m-d H:i:s"); // 2017-02-07 10:18:00 in UTC+2 timezone
You will store it like this:
$date_modified = time(); // 1486455515 which equals to 2017-02-07 08:18:00 in UTC
In the mysql query now() will take the server time so it is better to pass it using php
$date = date("Y-m-d H:i:s");
$time = strtotime($date);
$time = $time - (15 * 60);
$date = date("Y-m-d H:i:s", $time);
in model
$this->db->where('date_modified <' $date);
I am using an eloquent query to retrieve data from a table. The table columns look like this:
id started_at finished_at
1 2016-06-01 2016-06-30
2 2016-05-03 2016-05-28
What I want to do is, given a $date (ex: 2016-06-18 ) and get the data of the row, that the $date between started at and finished_at columns.
I have found whereBetween clauses in Laravel documentation, but I do not have an idea to use it correctly.
If you can use Carbon then this code will work fine for you.
$dateS = new Carbon('first day of January 2016');
$dateE = new Carbon('first day of November 2016');
$result = ModelName::whereBetween('created_at', [$dateS->format('Y-m-d')." 00:00:00", $dateE->format('Y-m-d')." 23:59:59"])->get();
Or you can check the issue by debugging query using DB::enableQueryLog(); and DB::getQueryLog(); functions like
DB::enableQueryLog();
$dateS = new Carbon('first day of January 2016');
$dateE = new Carbon('first day of November 2016');
$result = ModelName::whereBetween('created_at', [$dateS->format('Y-m-d')." 00:00:00", $dateE->format('Y-m-d')." 23:59:59"])->get();
var_dump($result, DB::getQueryLog());
Try to do something like this:
$date1 = Carbon::today()->toDateString();
$date2 = Carbon::today()->toDateString();
$myModel = MyModel::find(1);
$myModel->whereBetween('created_at', [$date1, $date2]);
$myModel->get();
Of course, you will need to change the dates.
You can use whereBetween something like this:-
ModelName::whereBetween('date',['2022-08-01','2022-10-30'])->get();
I am using laravel 4. But I am facing problem with finding the difference between two date: one coming from database table and another one is current datetime. From the date difference I am expecting 1 hour or 1 day. I've tried few solution but can't fix this yet. And also I don't know the better way to solve it. If you guys have any solution, please provide me an example. Please tell me if I need any library. Here is my code:
$lecture_id = Input::get('lecture_id');
$delegate_id = Input::get('delegate_id');
// $newDate = new Datetime();
$lecture = Lecture::find($lecture_id);
// $lec_date = Date::forge($lecture->start_time);
// $lec_date = new Datetime($lecture->start_time);
$lec_date = $lecture->start_time->diffForHumans(Carbon::now());
if ( $lec_date > 1) {
LectureDelegate::create(array(
'lecture_id' => Input::get('lecture_id'),
'delegate_id'=> Input::get('delegate_id')
));
return Redirect::to('/')->with('message', 'Your are successfully apply to the lecture');
}
Should be:
$lec_date = Carbon::createFromTimeStamp( strtotime( $lecture->start_time ) )->diffForHumans();
or possibly:
$lec_date = $lecture->start_time->diffForHumans();
If you add this to your Lecture.php model:
public function getDates()
{
return array('created_at', 'updated_at', 'deleted_at', 'start_time');
}
From the documentation:
By default, Eloquent will convert the created_at, updated_at, and
deleted_at columns to instances of Carbon...
You may customize which fields are automatically mutated, and even
completely disable this mutation, by overriding the getDates method of
the model.
As for diffForHumans the documentation states:
The lone argument for the function is the other Carbon instance
to diff against, and of course it defaults to now() if not specified.
update
If the timestamp from the database being passed to diffForHumans is in the future, Carbon automatically makes the return like:
When comparing a value in the future to default now:
1 hour from now
5 months from now
When comparing a value in the past to another value:
1 hour before
5 months before