I think Time object is just a mess. I really never learn how they works.
I have an array with data: 09:00-09:20 and 12:30-13:00.
Now i like to calculate the time between 09:00-09:20.
So i break up the array:
$break_1_dur = $usr_breaks['skift_rast1'];
//returns: 09:00-09:20
I break up the string:
$break_1_start = substr($break_1_dur,0,5);
//returns: 09:00
$break_1_ends = substr($break_1_dur,6,5);
//returns: 09:20
And now i'll use DateTime diff to calculate the time:
$break_1_dur = $break_1_start->diff($break_1_ends);
I have tried to make strings to "DateTime" with:
$break_1_start = new DateTime();
How can i in a easy way calculate this?
This should work for you:
Here I first split your array into the following structure with array_map():
Array
(
[skift_rast1] => Array
(
[start] => 09:00
[end] => 09:20
)
[skift_rast2] => Array
(
[start] => 12:30
[end] => 13:00
)
)
The I loop through all $times and calculate the difference with creating DateTime objects and get the difference via diff():
<?php
$usr_breaks = ["skift_rast1" => "09:00-09:20", "skift_rast2" => "12:30-13:00"];
$times = array_map(function($v){
return array_combine(["start", "end"], explode("-", $v));
}, $usr_breaks);
//print differences
foreach($times as $time) {
$timeOne = new DateTime($time["start"]);
$timeTwo = new DateTime($time["end"]);
$interval = $timeOne->diff($timeTwo);
echo sprintf("%d hours %d minutes<br>", $interval->h , $interval->i);
}
?>
output:
0 hours 20 minutes
0 hours 30 minutes
Related
I have a start_date of 1/10/2018, and an end_date of 1/8/2020, the difference between the two dates in months is 22, that is 1 year 10 months, now, I want to create tables that terminate at the end of each year as follows:
table 1
column_heading will be "1/10/2018 - 31/12/2018"
and the row will be "2 months"
table 2
column_heading will be "1/1/2019 - 31/12/2019"
and the row will be "12 months"
table 3
column_heading will be "1/1/2020 - 1/8/2020"
and the row will be "8 months"
I would like to loop something, maybe the difference between the dates to create the number of tables necessary, if the two dates exist within the same year it will only create 1 table, or 2 if it enters the next year, I am using laravel and carbon to manipulate the dates.
Thank you in anticipation of your help.
Something like this
Here's one way. Note that I had to convert the format of your dates to YYYY-mm-dd in order to use PHP date functions. In the end you'll get an array and it's easy for you to transform the final dates into the format you desire. You can test it here: https://www.tehplayground.com/lvuTdWl91TeItEQC
The code:
<?php
// example code
$s = "1/10/2018";
$e = "1/08/2020";
// reassemble so we can use the date functions YYYY-mm-dd
$s = implode("-", array_reverse(explode("/", $s)) );
$e = implode("-", array_reverse(explode("/", $e)) );
// get the parts separated
$start = explode("-",$s);
$end = explode("-",$e) ;
$iterations = ((intVal($end[0]) - intVal($start[0])) * 12) - (intVal($start[1]) - intVal($end[1])) ;
$sets=[$start[0] => array("start" => $s, "end" => "", "months" => 0)];
$curdstart= $curd = $s;
$curyear = date("Y", strtotime($s));
for($x=1; $x<=$iterations; $x++) {
$curdend = date("Y-m-d", strtotime($curd . " +{$x} months"));
$curyear = date("Y", strtotime($curdend));
if (!isset($sets[$curyear])) {
$sets[$curyear]= array("start" => $curdend, "end" => "", "months" => 0);
}
$sets[$curyear]['months']++;
$sets[$curyear]['end'] = date("Y-m-", strtotime($curdend)) . "31";
}
die(print_r($sets,1));
$mctr = 0 ;
The output:
Array
(
[2018] => Array
(
[start] => 2018-10-1
[end] => 2018-12-31
[months] => 2
)
[2019] => Array
(
[start] => 2019-01-01
[end] => 2019-12-31
[months] => 12
)
[2020] => Array
(
[start] => 2020-01-01
[end] => 2020-08-31
[months] => 8
)
)
Am trying to get hours from midnight from the current time
that is suppose now it's 02:34 then I expect to get
02:34 - 02:00,
01:00 - 02:00,
00:00 - 01:00
as an array
so I have tried
function getFromMidnight(){
$durations = [];
$currentTime = strtotime("now");
$iStartOfHour = $currentTime - ($currentTime % 3600);
$midnight = strtotime('today midnight');
$hours = floor(($iStartOfHour - $midnight)/3600);
array_push($durations,["from"=>$currentTime, "to"=>$iStartOfHour]);
if(floor(($iStartOfHour - $midnight)/3600) > 0){
for ($val = 1;$val <= $hours;$val++){
$newval = $val +=1;
array_push($durations, ["from"=>strtotime('- '.$val.' hours',$iStartOfHour),"to"=>strtotime('- '.$newval.' hours',$iStartOfHour)]);
}
}
return $durations;
}
For the first array has the correct durations e.g. from the above example 02:34-02:00 but the next arrays are messed up giving me wrong values with constant timestamps eg: 01:00 - 01:00
I suspect its my for loop with an error, what could be wrong?
I would not use that code, instead of working things out just use DateInterval inverted and work backwards. Then sub() the hour in the loop to get the offset.
<?php
date_default_timezone_set('UTC');
$begin = new DateTime('today midnight');
$end = new DateTime();
$interval = new DateInterval('PT60M');
$interval->invert = 1;
$daterange = new DatePeriod($begin, $interval, $end);
$range = [];
foreach ($daterange as $date){
$range[] = [
'from' => $date->format("H:i"),
'to' => $date->sub($interval)->format("H:i")
];
}
print_r($range);
https://3v4l.org/BMSbI
Result:
Array
(
[0] => Array
(
[from] => 00:00
[to] => 01:00
)
[1] => Array
(
[from] => 01:00
[to] => 02:00
)
[2] => Array
(
[from] => 02:00
[to] => 03:00
)
[3] => Array
(
[from] => 03:00
[to] => 04:00
)
)
first of all, I'm sorry for my english. I'm from germany.
Now my Problem:
I have a multiple array with some dates in it. I had to filter the first and the last date for every IP because I need the difference of both dates to know how much time the User on my website.
I did that and got all I need. Here is a part of my code output:
$ip_with_dates:
Array
(
[0] => Array
(
[ip] => 72.xx.xx.xx
[first_date] => 2015-10-12 00:10:15
[last_date] => 2015-10-12 01:10:51
)
[1] => Array
(
[ip] => 85.xx.xx.xx
[first_date] => 2015-10-12 00:10:19
[last_date] => 2015-10-12 01:10:56
)
I tried to get the time between those two dates with:
$visit_lenght = [];
foreach($ip_with_date as $key => $val){
$date1 = new DateTime($val['first_date']);
$date2 = new DateTime($val['last_date']);
$interval = $date1->diff($date2)->format('%h %m %s');
$visit_lenght[] = $interval;
}
what gives me this output:
Array
(
[0] => 1 36
[1] => 1 37
[2] => 0 3
[3] => 0 9
)
well but this isn't good to work with. I need the time in seconds not in H:m:s
but I really don't now how. This is a part of my project where I'm really fighting with. Maybe someone of you could help me with this.
I'm working with laravel. Normal PHP would make it too but if someone knows a solution in laravel, it would be nice as well!
thanks for any help!
To get time diff in seconds you need to convert your datetime objects to timestamps:
$visit_lenght = [];
foreach($ip_with_date as $key => $val){
$date1 = new DateTime($val['first_date']);
$date2 = new DateTime($val['last_date']);
$interval = $date1->getTimestamp() - $date2->getTimestamp()
$visit_lenght[] = $interval;
}
You may get timestamps of two dates and count the difference.
something like (in php).
$date1t = $date1->getTimestamp();
$date2t = $date2->getTimestamp();
$diff = $date2t - $date1t;
http://php.net/manual/en/datetime.gettimestamp.php
Try this way (general/basic way)
$visit_lenght = [];
foreach($ip_with_date as $key => $val){
$date1 = strtotime($val['first_date']);
$date2 = strtotime($val['last_date']);
//$interval = $date1->diff($date2)->format('%h %m %s');
$interval = $date2 - $date1;
$visit_lenght[] = $interval;
}
you can make this:
->format('Y-m-d H:i:s')
With DateTime() class of PHP, it is super simple to find difference between time. Here is an exmple:
<?php
$ip = "xxxx-xxx-xxx";
$t1 = DateTime::createFromFormat('Y-m-d H:i:s', '2015-10-12 00:10:15');
$t2 = DateTime::createFromFormat('Y-m-d H:i:s', '2015-10-12 01:10:51');
echo "User from IP {$ip} spent " . $t1->diff($t2)->format("%h hours, %i minutes and %s seconds");
?>
I'm making a CRM-system, and I now need to offer people to make recurring events. For that they will have to fill out if it's daily, weekly, monthly or yearly, and everything like when you do it in Google Calendar.
But how will I get the dates in an array for "monday next 3 weeks" for example?
$int_count = 3; // How many to repeat
$date = new \DateTime('next monday');
$result = array($date->format('Y-m-d'));
for ($i=1; $i<$int_count; $i++) {
$result[] = $date->modify('+1 week')->format('Y-m-d');
}
print_r($result);
Result:
Array
(
[0] => 2015-01-26
[1] => 2015-02-02
[2] => 2015-02-09
)
The best way is to use DateTime and DatePeriod classes. It's the most correct way to deal with dates now. It deals with timezones and DST shifts automatically. It's just the way you must do it.
$daterange = new DatePeriod(new DateTime('next monday'), new DateInterval('P1W'), 2);
$dates = [];
foreach($daterange as $date) $dates []= $date->format("Y-m-d H:i:s");
print_r($dates);
The result will be:
Array
(
[0] => 2015-01-26 00:00:00
[1] => 2015-02-02 00:00:00
[2] => 2015-02-09 00:00:00
)
I need your help in how to subtract the last_modified and the final_manuscript_date in the following array:
Array (
[chapters_id] => 10736
[last_modified] => 2010-12-21 15:01:55
[steps_id] => 3
[sub_step_id] => 0
[steps_position] => 1
[final_manuscript_date] => 2010-09-27
)
So I can in this case get a value of N days between the dates 2010-12-21 and 2010-09-27?
Can't you simply do:
$diff = strtotime($arr["final_manuscript_date"]) - strtotime($arr["last_modified"]);
$days = $diff / 84600; // to get # of days, you can round them off or use ceil/floor
If you have 5.3+:
$date1 = new DateTime("2010-09-27");
$date2 = new DateTime("2010-12-21");
$interval = $date1->diff($date2);
echo $interval->d //returns days.
Have you checked strtotime?
http://php.net/manual/en/function.strtotime.php