codeigniter - save current date - codeigniter

I need to save current date and I supposed I must modify this code into application\libraries\grocery_crud.php
line # 253
case 'date':
/*if(!empty($value) && $value != '0000-00-00' && $value != '1970-01-01')
{
list($year,$month,$day) = explode("-",$value);
$value = date($this->php_date_format, mktime (0, 0, 0, (int)$month , (int)$day , (int)$year));
}
else
{
$value = '';
}*/
$value = // some code with current date
It's in that way? or maybe there is another solution, hope can help me, thanks in advance!

Sometimes you may not be able to change the column type from datetime to timestamp.
On these scenarios, considering a GroceryCrud application, there are two possible (and very easy) solutions:
1) Use callback_insert, create your custom insert logic and set the value for the datetime column using a function like NOW() or GETDATE() depending on the DB your are using: $this->db->set('dateTimeColumnName', 'NOW()', FALSE);
2) You can use the callback_before_insert, and inside it, you can call the function date() from PHP, and set there the current date for your insert in the respective array or object attribute that will be passed / returned for the auto-insert be processed right after the callback´s execution

Related

Input date value and compare with existing table

I'm trying to develop a program with Laravel. I have created a table ‘tbl_holiday’ like below:
database table
I have created a form containing 02 input (date) types named ‘from_leave’ and ‘to_leave’.
I want to check these 2 inputs value(date) with the database table ‘tbl_holiday’ having the condition:
If the previous day of ‘from_leave’ match with ‘tbl_holiday.holiday_date’
Or next day of ‘to_leave’ match with ‘tbl_holiday.holiday_date’
The message “Invalid”.
Else
Message “Valid”.
Thanks in advance.
You can simply use wherebetween and check the number of data available in the collection.
$from = $request->from_date;
$to = $request->to_date;
$dates = YourModel::whereBetween('holiday_date', [$from, $to])->get();
if($dates->count() == 0){
return "valid";
}else{
return "Invalid";
}

Laravel - Date variable doesn't keep its original value

here's a rough sample of my actual code:
$start_date = '01/02/2003';
for($i=1 ; $i<3 ; $i++){
$expiration_date = $start_date->addMonth(12);
}
dump($expiration_date ); //outputs 01/02/2005
dd($start_date); //outputs 01/02/2005
My Question is that why is my start_date variable also becomes the same with the expiration_date variable.
I need the start date at the end of the loop to still have its original value which is 01/02/2003.
When you run addMonth methods against a Carbon object it updates the object itself.
Here's what you need to do:
$expiration_date = $start_date->copy()->addMonth(12);
The copy creates a new Carbon object
Check out the documentation

Delete a row in databse if has a code that has been present for 15 min

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);

Talend: Save variable for later use

I´m trying to save a value in spreadsheet's header for later use as a new column value.
This is the reduced version with value (XYZ) in header:
The value in header must be used for new column CODE:
This is my design:
tFilterRow_1 is used to reject rows without values in A, B, C columns.
There is a conditional in tJavaRow_1 to set a global variable:
if(String.valueOf(row1.col_a).equals("CODE:")){
globalMap.putIfAbsent("code", row1.col_b);
}
The Var expression in tMap_1 to get the global variable is:
(String)globalMap.get("code")
The Var "code" is mapped to column "code" but I'm getting this output:
a1|b1|c1|
a2|b2|c2|
a3|b3|c3|
What is missed or there is a better approach to accomplish this escenario ?
Thanks in advance.
Short answer:
I tJavaRow use the input_row or the actual rowN in this case row4.
Longer answer, how I'd do it.
I'd do is let the excel flow in AS-IS. By using some Java tricks we can simply skip the first few rows then let the rest of the flow go through.
So the filter + tjavarow combo can be replaced with a tJavaFlex.
tJavaFlex I'd do:
begin:
boolean contentFound = false;
main
if(input_row.col1 != null && input_row.col1.equalsIgnoreCase("Code:") ) {
globalMap.put("code",input_row.col2);
}
if(input_row.col1 != null && input_row.col1.equalsIgnoreCase("Column A:") ) {
contentFound = true;
} else {
if(false == contentFound) continue;
}
This way you'll simply skip the first few records (i.e header) and only care about the actual data.

How to get datetime differance in laravel 4

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

Resources