add two time values and comparing with other time - php

i have two time values say starttime=23:00:00 and estimation time=00:45:00 now i want to add starttime and estimation time and store it in a variable i did like this
$add = abs(strtotime($starttime) + strtotime($estimationtime));
and now i have to compare a new time say currenttime or any other time interval (23:50:00)
if($time >=$starttime && $time <=$add)
{
$message=array("message"=>"there is an appointment");
}
if i echo $add i am not getting the right format it shows 2675756700 pls help me to solve this

If you want to echo your $add variable in a readable form you have to format it first.
echo(date("H:i:s", $add));

strtotime() returns a timestamp, which is the number of seconds since Jan 1/1970. You cannot compare this to a formatted time value - it's just an integer.
So basically, you're doing the equivalent of:
$add = 1 + 2; // 3
if (3 >= '23:50:00') ...
if you want to compare time values as timestamps, you'll have to convert EVERYTHING to timestamps. Or, you could use the DateTime object, which'll handle a lot of this sort of thing for you automatically.

Related

converting H:i:s to minutes only but array_map() giving errors

I am trying to convert a long datatype data to time in which I am successful.
In each session time array I have values like ["1276999","787878","677267"]. I passed this array in the array_map function and converted to time which is working.
Now within the the convert_time function I am calling another function, using array_map which will convert each time (i.e 1:40:00 to 100 minutes) but the issue is my 2nd array map function which is giving me error that array_map needs 2nd parameter to be an array...
$each_session_time = array();
for ($i=0; $i<sizeof($array_in_time_str) ; $i++) {
$each_session_time[$i]=$array_out_time_str[$i]-$array_in_time_str[$i];
}
//session time in hours
array_map("convert_to_time", $each_session_time);
function convert_to_time($each_session) {
# code...
$each_sess_time=array();
$each_sess_time=date("H:i:s",$each_session);
array_map("get_minutes",$each_sess_time);
return $each_sess_time;
}
function get_minutes($session_time) {
// algorithm to convert each session time to minutes)
}
You need to move out the array_map("get_minutes",$each_session_time); from the convert_to_time function.
example:
<?php
$each_session_time=["1276999","787878","677267"];
//session time in hours
$times = array_map("convert_to_time", $each_session_time);
$minutes = array_map("get_minutes",$times);
function convert_to_time($each_session)
{
# code...
$each_sess_time=array();
$each_sess_time=date("H:i:s",$each_session);
return $each_sess_time;
}
function get_minutes($session_time)
{
//algo to convert each session time to minutes)
}
print_r($minutes);
It seems you are starting with valid timestamps - seconds passed since January 1, 1970 - so to get the difference between two values in minutes, you just have to subtract one from the other and multiply it by 60.
If you want more control over your data, for example to format it differently later on, I would recommend using DateInterval objects instead of the difference between two timestamps and strings that you are using now. Note that the difference between two timestamps is not a valid timestamp itself so you cannot use date() to format it.
considering you are working with strings like "XX:YY:ZZ" you can try
$split = explode(":",$session_time); $minutes = $split[1];
to get the "i" part of the string.
You could also use a dateTime object (http://php.net/manual/en/class.datetime.php) by doing new DateTime($each_session); in the first loop and using DateTime::format("H:i:s") and DateTime::format("i") on that object depending on what data you need

SQL/PHP Compare two dates as seconds

I have a field called lastTime which holds the current time when I make a query to my table using NOW().
It's stored as datetime in my table.
I then retrieve this value at some point from the table with a query and it returns a string.
$lastTime = retrieve_lasttime_from_table();
$curTime = date("Y-m-d H:i:s");
if($curTime - $lastTime >= 5){
//Do stuff here
}
So basically I'm trying to see if 5 seconds have passed.
What's the correct way to do this?
strtotime() is the best way to find the difference
if((strtotime($curTime) - strtotime($lastTime)) >= 5){
//Do stuff here
}
You can use strtotime and below example would probably work.
if(strtotime($lastTime) - (strtotime($curTime)) >= 5){
if((strtotime($curTime) - strtotime($lastTime)) >= 5){
//Do stuff here
}
Note: I switched the positions of $lastTime and $curTime, as I guess you need to check, when Current Time is greater than or equal to 5 seconds from Last time.

Php Time Format

I have here a mysql query that get the average of the column(the column data type is 'time'). The column values for example are:
00:00:55, 00:00:59, 00:01:03
SELECT AVG(TIME_TO_SEC(column_name)) FROM table_name)AS average_result
In my Php I formatted the result this way:
<?php foreach($display_average as $da){
echo date("H:i:s", ($da["average_result"]));
}
?>
Outputs: 08:00:59 instead of 00:00:59, Why does this starts with 08? Or did I miss something? Thanks!
Both PHP's date/time functions and MySQL's date/time data types handle wall clock timestamps, not durations; i.e. 00:00:55 means fifty-five seconds past midnight. This is not what you want; you couldn't handle durations longer than 23 hours, 59 minutes, 59 seconds, because the data types and functions you're using are handling clock time, which cannot exceed these values.
Your specific issue stems from timezone settings. Your larger issue is that you need to store simple integer values expressing elapsed seconds or minutes; not timestamps. To format that into a human readable string in PHP you can use the DateInterval class.
see php manul, about the date_default_timezone_set your timezone is +8
the default date.timezone of PHP is utc, u can change it to date.timezone = PRC
date_default_timezone_set('UTC');
echo date("H:i:s", 59);//00:00:59
//date_default_timezone_set('RPC');
//echo date("H:i:s", 59);//08:00:59
Always go for standard/formal approaches. But if anyhow you need it custom, then you can do almost everything with programming. Here we go
Get your time as numbers (number of seconds in your time filed) from database as
SELECT
AVG
(
HOUR(column_name) * 3600
+ MINUTE(column_name) * 60
+ SECOND(column_name)
) AS numeric_average_result FROM table_name
Now you can convert number of seconds to proper time as
foreach($display_average as $da)
{
$r = numToTime($da["numeric_average_result"]);
echo "<br>".$r;
}
function numToTime($num)
{
$seconds = $num%60;
$num = (int)($num/60);
$minutes = $num%60;
$hours = (int)($num/60);
return make2digit($hours).":".make2digit($minutes).":".make2digit($seconds);
}
function make2digit($val)
{
if(strlen($val) == 1)
return "0".$val;
else
return $val;
}

php strtotime() values not working as expected

this code keeps telling me that $lasUpdate is always greater than $yesterday no matter the change i make to $yesterday result is (12/31/14 is greater than 01/19/15 no update needed). i feel like i'm missing something simple thank you in advance it is greatly appreciated.
$result['MAX(Date)']='12/31/14';
$lastUpdate = date('m/d/y', strtotime($result['MAX(Date)']));
$yesterday = date('m/d/y', strtotime('-1 day'));
if($lastUpdate<$yesterday){echo $lastUpdate.'is less '.$yesterday.'<br>'.'update needed';}
if($lastUpdate>=$yesterday){echo $lastUpdate.'is greater than '.$yesterday.'<br>'.'no update needed';
You have fallen victim to PHP type juggling with strings. A date function has a return value of a string. You cannot compare dates in their string format since PHP will juggle strings into integers in the context of a comparison. The only exception is if the string is a valid number. In essence, you are doing:
if ('12/31/14' < '01/19/15') { ... }
if ('12/31/14' >= '01/19/15') { ... }
Which PHP type juggles to:
if (12 < 1) { ... }
if (12 >= 1) { ... }
And returns false on the first instance, and true on the second instance.
Your solution is to not wrap date around the strtotime functions, and just use the returned timestamps from the strtotime functions themselves to compare UNIX timestamps directly:
$lastUpdate = strtotime($result['MAX(Date)']);
$yesterday = strtotime('-1 day');
You will however want to use date when you do the echo back to the user so they have a meaningful date string to work with.
Try something like this:
$lastUpdate = strtotime($result['MAX(Date)']);
$yesterday = strtotime('-1 day');
if ($lastUpdate < $yesterday) { /* do Something */ }
12/31/14 is greater than 01/19/15
Because 1 is greater than 0. If you want to compare dates that way you will need to store them in a different format (from most to least significant digit), for example Ymd.
Or store the timestamps you are making in the different variables and compare them.

Getting a time difference in mongoDB?

I'm using PHP + mongoDB.
How can I get a time difference between two time values?
I have a real time value which is string
$realtime = "2010-01-01 12:00:00";
and another value which is unixstamp time,
$mongotime = new Mongodate(strtotime($realtime));
So I can use either a string time value or unix time stamp.
But I'm not sure the way to get time difference between two values.
Should I just subtract two $mongotime values and does it give me a time difference in seconds?
If you have 2 unix timestamps...
$date = $item['pubdate'];
(etc ...)
$unix_now = time();
$result = strtotime($date, $unix_now);
$unix_diff_min = (($unix_now - $result) / 60);
$min = round($unix_diff_min);
This will give number of mins between the 2 timestamps...

Categories