how to save the results of the date difference to be saved to the database ?
public function save(){
$item = $this->input->post('item');
$date_in = $this->input->post('date_in'); //date in
$date_out = $this->input->post('date_out'); // date out
$hasil = $date_in->diff($date_out);
//echo $hasil->format('%a'); I will save these results to the database
$data_insert = array(
'item' => $item,
'date_in' => $date_in,
'date_out' => $date_out,
'selisih' => $hasil
);
//url save
}
String don't have method diff, diff is the method of DateTime, you need to change string to DateTime.
And it will return DateInterval object, you can get the total days by format('%a') and insert to database;
$hasil = (new DateTime($date_in))->diff(new DateTime($date_out));
$hasil = $hasil->format('%a');
$data_insert = array(
'item' => $item,
'date_in' => $date_in,
'date_out' => $date_out,
'selisih' => $hasil
);
$this->db->insert('mytable', $data_insert);
If you want to get difference you can get it with Carbon easily using diffInDays Example code for you :
$first = \Carbon\Carbon::createFromFormat('Y-m-d H:s:i', $date_in);
$second = \Carbon\Carbon::createFromFormat('Y-m-d H:s:i', $date_out);
$diff_in_days = $second->diffInDays($first);
Or get it without Carbon :
$first = new DateTime($date_in);
$second = new DateTime($date_out);
$interval = $first->diff($second);
Related
I'm trying to work out how I can remove all timestamps which have a certain reservation id after them.
Currently I can unset only based on timestamp and not check whether reservation id is there. This is part of the php code snippet:
$pending_prep_dates_array = array();
$check_in_date = get_post_meta( $resID, 'reservation_checkin_date', true );
$check_out_date = get_post_meta( $resID, 'reservation_checkout_date', true );
$check_in_prep = new DateTime($check_in_date);
$check_in_prep->sub(new DateInterval('P2D')); // P1D means a period of 1 day
$check_in_final = $check_in_prep->getTimestamp();
$check_out_prep = new DateTime($check_out_date);
$check_out_prep->add(new DateInterval('P2D')); // P1D means a period of 1 day
$check_out_final = $check_out_prep->getTimestamp();
for ($currentDate = $check_in_final; $currentDate <= $check_out_final; $currentDate += (86400)) {
$Store = $currentDate;
unset($pending_prep_dates_array[$Store]);
This is how it is stored in array in my database:
a:15{i:1652054400;i:8997;i:1652140800;i:8999;i:1652227200;i:8999;i:1652313600;i:8999;i:1652400000;i:8999;i:1652486400;i:8999;i:1652572800;i:8999;i:1651536000;i:8993;i:1651622400;i:8993;i:1651708800;i:8993;i:1651795200;i:8993;i:1651881600;i:8993;i:1651968000;i:8997;i:1651363200;i:8993;i:1651449600;i:8993;}
So to clarify, how can I only remove timestamps if reservation id is for example 8999?
Thanks
You can iterate through the data and build a new array with the ID(s) removed:
<?php
$raw = 'a:15:{i:1652054400;i:8997;i:1652140800;i:8999;i:1652227200;i:8999;i:1652313600;i:8999;i:1652400000;i:8999;i:1652486400;i:8999;i:1652572800;i:8999;i:1651536000;i:8993;i:1651622400;i:8993;i:1651708800;i:8993;i:1651795200;i:8993;i:1651881600;i:8993;i:1651968000;i:8997;i:1651363200;i:8993;i:1651449600;i:8993;}';
$data = unserialize($raw);
$ids_to_remove = [8999, 8993];
$data_with_id_removed = [];
foreach ($data as $timestamp => $id) {
if (in_array($id, $ids_to_remove))
continue;
$data_with_id_removed[$timestamp] = $id;
}
var_dump($data_with_id_removed);
Result (all timestamps with id 8999 and 8993 removed)
array(2) {
[1652054400]=>
int(8997)
[1651968000]=>
int(8997)
}
I am trying to insert/update +/- 10k rows with a foreach loop. The complete loop duration is about 3-5minutes. Are there any tips on my code to do the insertion of update faster? The $rows are retrieved from a xls file converted to domdocument.
foreach($rows as $key => $row)
{
if($key < 1){continue;}
$cells = $row -> getElementsByTagName('td');
foreach ($cells as $cell) {
$project_id = $cells[0]->nodeValue;
$title = $cells[1]->nodeValue;
$status = $cells[2]->nodeValue;
$projectmanager = $cells[3]->nodeValue;
$engineer = $cells[4]->nodeValue;
$coordinator = $cells[5]->nodeValue;
$contractor_a = $cells[6]->nodeValue;
$contractor_b = $cells[7]->nodeValue;
$gsu = $cells[9]->nodeValue;
$geu = $cells[10]->nodeValue;
$query = $this->Projects->find('all')->select(['project_id'])->where(['project_id' => $project_id]);
if ($query->isEmpty()) {
$project = $this->Projects->newEntity();
$project->title = $title;
$project->project_id = $project_id;
$project->status = $status;
$project->projectmanager = $projectmanager;
$project->engineer = $engineer;
$project->coordinator = $coordinator;
$project->contractor_a = $contractor_b;
$project->contractor_b = $contractor_a;
$project->gsu = date("Y-m-d H:i:s");
$project->geu = date("Y-m-d H:i:s");
$project->gsm = date("Y-m-d H:i:s");
$project->gem = date("Y-m-d H:i:s");
if ($this->Projects->save($project)) {
//$this->Flash->success(__('The project has been saved.'));
continue;
}else{
debug($project->errors());
}
}else{
continue;
$query->title = $title;
$query->status = $status;
$query->projectmanager = $projectmanager;
$query->engineer = $engineer;
$query->coordinator = $coordinator;
$query->contractor_a = $contractor_b;
$query->contractor_b = $contractor_a;
$query->gsu = $gsu;
$query->geu = $geu;
if ($this->Projects->save($query)) {
//$this->Flash->success(__('The project has been saved.'));
continue;
}
}
}
//$this->Flash->error(__('The project could not be saved. Please, try again.'));
}
For faster bulk inserts don't use entities but rather generate insert queries directly.
https://book.cakephp.org/3.0/en/orm/query-builder.html#inserting-data
Ello, my vriend.
The TableClass->save() method is useful when saving one single record, in your case, you should use TableClass->saveMany() instead.
For this to happen, you need to treat your entities as arrays inside your foreach.
After the foreach, you will use another method from the tableclass (newEntities) to convert the array into entities before finally save them.
Basic example:
//Lets supose our array after our foreach become something like this:
$all_records =
[
//Each item will be an array, not entities yet
[
'name' => 'I.N.R.I.',
'year' => '1987',
'label' => 'Cogumelo',
'country' => 'Brazil',
'band' => 'Sarcófago',
'line_up' => '[{"name":"Wagner Antichrist","role":"Vomits, Insults"},{"name":"Gerald Incubus","role":"Damned Bass"},{"name":"Z\u00e9der Butcher","role":"Rotten Guitars"},{"name":"D.D. Crazy","role":"Drums Trasher"}]'
],
//Another record coming in..
[
'name' => 'Eternal Devastation',
'year' => '1986',
'label' => 'Steamhammer',
'country' => 'Germany',
'band' => 'Destruction',
'line_up' => '[{"name":"Marcel Schmier","role":"Vocals, Bass"},{"name":"Mike Sifringer","role":"Guitars"},{"name":"Tommy Sandmann","role":"Drums"}]'
]
];
//Time to get the tableclass...
$albums = TableRegistry::get('Albums');
//Time to transform our array into Album Entities
$entities = $albums->newEntities($all_records);
//Now, we have transformed our array into entities on $entities, this is the variable we need to save
if(!$albums->saveMany($entities))
{
echo "FellsBadMan";
}
else
{
echo "FellsGoodMan";
}
You can read more about here
I'm looking for a way to convert a date from a json feed (i am unable to change how the date is passed in the feed)
The date is passed as a string
'BirthDate' => string '/Date(610776000000-0400)/' (length=25)
How do i parse this to a date that i can persist with doctrine?
(BirthDate is set-up as a datetime field)
$em = $this->getDoctrine()->getManager();
$thingyRepo = $em->getRepository('DataBundle:Thingy');
$entry = new Thingy();
$entry->setBirthDate(this->parseDate($myData['BirthDate']));
$em->persist($entry);
$em->flush();
From Rufines comment i created a function to parse the date for me
public function parseDate($dateString)
{
if ($dateString) {
$match = preg_match( '/\/Date\((\d+)([-+])(\d+)\)\//', $dateString, $date );
$timestamp = $date[1] / 1000;
$operator = $date[2];
$hours = $date[3] * 36; // Get the seconds
$datetime = new \DateTime();
$datetime->setTimestamp( $timestamp );
//deal with the offset
$datetime->modify( $operator.$hours.' seconds' );
$result = $datetime->format( 'd-m-Y H:i:s' )
//var_dump( $result );
return ($result);
}
return(null);
}
I am trying to make a function that grabs all the days that have events that are in a database for a certain user. For instance if there were two events on Jan 23, 2013 it would add Jan 23, 2013 to the array. I got it to work so it adds all the days (without adding the same day twice) but now I want to be able to say how many dates are on each day. So on Jan 23, 2013 it would say they have two events in that day.
I hope this makes sense... I have some code for further aid.
PHP Function (grabbing each day that has events)
//gets upcoming days that have events for a user
public function get_upcoming_days_with_events() {
$return_array = array();
$date_in=null;
$user_id = $this->session->userdata('id');
$query =$this->db->select()->from('events')->where('user_id', $user_id)->get();
foreach ($query->result_array() as $event => $row) {
$date = strtotime($row['date_due']);
if (sizeof($return_array) != 0) {
foreach ($return_array as $date_in_array => $row) {
$d = $row['full_date'];
if (date('Y-m-d', $date) == $d) {
//date is already in array
//increment the number of assignments on this day
$row['number_of_assignments'] += 1;
$date_in = true;
} else{
$date_in = false;
}
}
}
if ($date_in == false) {
$return_array[] = array(
'day' => date('d', $date),
'month' => date('m', $date),
'full_date' => date('Y-m-d', $date),
'number_of_assignments' => 1
);
}
}
return $return_array;
}
So with this I want to be able to increment $return_array['number_of_assignments'] if my function notices that a certain day has more than one event.
Let me know if you need any more info...
Thanks! :)
We can save the info in return_array by index of date, if the date info have not been set into return_array, we make an empty info. Each time, we simply increase number_of_assignments.
public function get_upcoming_days_with_events()
{
$return_array = array();
$user_id = $this->session->userdata('id');
$query =$this->db->select()->from('events')->where('user_id', $user_id)->get();
foreach ($query->result_array() as $event => $row)
{
$date = strtotime($row['date_due']);
$date_key = date('Y-m-d', $date);
if (!isset($return_array[$date_key]))
{
$new_item = array(
'day' => date('d', $date),
'month' => date('m', $date),
'full_date' => $date_key,
'number_of_assignments' => 0,
);
$return_array[$date_key] = $new_item;
}
$return_array[$date_key]['number_of_assignments']++;
}
$return_array = array_values($return_array);
return $return_array;
}
Function: array_count_values() can help you with this, it gives total number of occurance of a value in an array.
For example:
$a = array("apple", "banana", "apple");
var_dump(array_count_values($a));
Will output
array(
[apple] => 2,
[banana] => 1
);
So instead of trying to filter out duplicate events, add them all on the array and then use array_count_values() at last to know their occurenses.
I am writing a PHP script where the inputs are:
From date
To date
I then want to take that date range and create an array of some sort that has:
Array(date,x)
As I add each date to the array, I will calculate the value that goes with it.
With .NET I would (off the top of my head) use a dictionary where the date was the key and x is the value.
The main task is getting that date range, then splitting that range into an array or dictionary of some sort (What would be ideal in php)
As i'm adding it to the array i'll run off some other code i've already wrote that calculate the value to go with that date
At the end of it all when it's added in there, i'll need to iterate through the array or list and add all the x values together.
(Untested)
function dateArray($from, $to, $value = NULL) {
$begin = new DateTime($from);
$end = new DateTime($to);
$interval = DateInterval::createFromDateString('1 day');
$days = new DatePeriod($begin, $interval, $end);
$baseArray = array();
foreach ($days as $day) {
$dateKey = $day->format("Y-m-d");
$baseArray[$dateKey] = $value;
}
return $baseArray;
}
$datesArray = dateArray('2011-01-01', '2011-03-31',true);
you can try this
function makeDateRange($from,$to,$pattern='m-d-y')
{
$day = date( $pattern , $from );
$to = date( $pattern , $to );
$parseDate = date_parse($from);
while( $day <= $to ) {
$day = mktime(0,0,0,$parseDate["month"],$parseDate["day"]+1,$parseDate["year"]);
$dateArray[] = date($pattern , $day);
}
return $dateArray;
}
// here make array
$keys = makeDateRange("12-01-11","12-02-11");
//here make above array as key in $a array
$a = array_fill_keys($keys, 'none');
print_r($a);
If I understand you correctly, you could use an associative array for that:
array(
'00-00-00' => $value,
'01-01-01' => $value,
// etc...
);
Or you can create it like this:
$myArray = array();
$myArray['00-00-00'] = $value;
$myArray['01-01-01'] = $value;
You could populate them by running a loop...