I want to convert two integer as a time like:
$hour = 13;
$minute = 45;
---> $result = 13:45:00 //(In TimeFormart)
after I combine that i have to do some SQL Querys and PHP Calculations
can somebody help me?
Even when your question is very difficult to understand. Nobody knows what exactly you want to do and in which way and volume.
The only correct way to do it is this way:
$hour = 13;
$minute = 45;
$seconds = 0;
$result = date('H:i:s', mktime($hour, $minute, $seconds));
echo $result;
// output: 13:45:00
Important: When using it for making a time, you must pass hour, minute, seconds - if you dont pass seconds it takes the current time seconds.
You can even pass day, month, year for more information check out the PHP Reference.
https://www.php.net/manual/de/function.mktime.php
Try this code may be it help you.
<?php
$hour = 13;
$minute = 45;
$second=35;
// $result = 13:45:00 //(In TimeFormart)
$result = $hour.":".$minute.":".$second;
echo $result;
?>
here's what you want, but if you put more than 24 in hour, it'll accept, it just returns you how many hours, minutes and seconds not that actual time
$hour = 13;
$minute = 45;
echo date($hour.':'.$minute.':s');
$hour = 13;
$minute = 45;
$result = $hour . ':' . $minute . ':' . '00';
echo $result;
output---> 13:45:00 //(In TimeFormart)
Related
i have two different break time
default break time
extra break time
here i want to sum of two times and display 12 hrs format
EX :
$default_time = "00:30";
$extra_time = "00:25";
my expected output : 00:55
but now display 01:00
this is my code
$default_time = $work_data->break_time;
$break_time = $work_data->extra_time;
$total_break = strtotime($default_time)+strtotime($break_time);
echo date("h:i",strtotime($total_break));
Here is the function you can calculate total time by passing the arguments to functions.
$hours, $min are supposed variable which is zero
$default_time = "00:30";
$break_time = "00:25";
function calculate_total_time() {
$i = 0;
foreach(func_get_args() as $time) {
sscanf($time, '%d:%d', $hour, $min);
$i += $hour * 60 + $min;
}
if( $h = floor($i / 60) ) {
$i %= 60;
}
return sprintf('%02d:%02d', $h, $i);
}
// use example
echo calculate_total_time($default_time, $break_time); # 00:55
There is one function call to strtotime function too much.
You should leave out the strtotime() call in the last line, as $total_break already is a UNIX timestamp:
$total_break = strtotime($default_time)+strtotime($break_time);
echo date("h:i",$total_break);
The problem is that you're trying to add too specific timestamps, but what you're trying to achieve is adding two durations. So you need to convert those timestamps into durations. For that you need a base, which in your case is 00:00.
$base = strtotime("00:00");
$default_time = $work_data->break_time;
$default_timestamp = strtotime($default_time);
$default_duration = $default_timestamp - $base; // Duration in seconds
$break_time = $work_data->extra_time;
$break_timestamp = strtotime($break_time);
$break_duration = $break_timestamp - $base; // Duration in seconds
$total_break = $default_duration + $break_duration; // 55 min in seconds
// If you want to calculate the timestamp 00:55, just add the base back to it
echo date("H:i", $base + $total_break);
Consider using standard DateTime and DateInterval classes. All you will need is to convert your second variable value to interval_spec format (see http://php.net/manual/en/dateinterval.construct.php for details):
$defaultTime = "00:30";
$breakTime = "PT00H25M"; // Or just 'PT25M'
$totalBreak = (new DateTime($defaultTime))->add($breakTime);
echo $totalBreak->format('H:i');
You could try the following code fragment:
$time1 = explode(":", $default_time);
$time2 = explode(":", $break_time);
$fulltime = ($time1[0] + $time2[0]) * 60 + $time1[1] + $time2[1];
echo (int)($fulltime / 60) . ":" . ($fulltime % 60);
<?php
$time = "00:30";
$time2 = "00:25";
$secs = strtotime($time2)-strtotime("00:00:00");
$result = date("H:i:s",strtotime($time)+$secs);
print_r($result);
?>
Use below code you will definitely get your answers.
$default_time = "00:30:00";
$extra_time = "00:25:00";
$secs = strtotime($extra_time)-strtotime("00:00:00");
$result = date("H:i:s A",strtotime($default_time)+$secs);
echo $result;die;
You can modify above code as per your need.
You could try the following:
$default_time = $work_data->break_time;
$date_start = new DateTime($default_time);
$break_time = $work_data->extra_time;
$interval = new DateInterval("PT" . str_replace(":", "H", $break_time) . "M");
$date_end = $date_start->add($interval);
echo $date_end->format("H:i");
Note that this doesn't account for times which span a 24 hour period
I'm looking for a posibility to set minutes to "00" instead of "60". I have following code:
$minute = date('i');
$minutesround = round($minute / 10) * 10;
$hour = date('H');
$minutesround = str_pad($minutesround, 2, 0, STR_PAD_LEFT);
$time = $hour . $minutesround;
echo $time;
My problem: If it is eg. 4:56pm the output should like this: 1700
but in my case, I get this 1660
Someone has an idea, how to realize this?
Thank you!
This works well:
$myRoundedTime = round(time()/600) * 600; // round time to nearest 6th of an hour
$time = date('Hi', $myRoundedTime);
echo $time;
A working example: http://sandbox.onlinephpfunctions.com/code/b2b595683d9adf8e5273cbdef775bf69b6c036d4
A simple if statement should do the trick
$hour = date('H');
if($minutesround == 60) {
$minutesround = 0;
$hour++;
if($hour == 24) $hour = '00';
}
Starting with the number 9 and using php, I would like to be able to count up from there, and echo out the next number in increments of 1.
So, number 9, then after 1 month the number would change to 10, then another month 11, then 12 etc., with no maximum number/stop point.
How can I accomplish this? So far I have the below code.
$number = 9;
$output = $number + 1;
echo $output;
Is there a way to set this to increase once a month?
You can do this with the PHP date()-function. This is one example of doing it if you are not dependent on the day of the month, but adding day functionality is possible and should be quit easy.
$startNumber = 9;
$startYear = 2015;
$startMonth = 9;
$currentYear = intval( date( "Y" ) );
$currentMonth = intval( date( "n" ) );
$monthsToAdd = ( ( $currentYear - $startYear ) * 12 )
+ ( $currentMonth - $startMonth );
echo $startNumber + $monthsToAdd;
From your question, I'd say:
$number = 9;
$output = date('n') + $number;
echo $output;
But that depends on what you are trying to accomplish. You can also wrap the number around the date() with a modulo.
However this is nothing random. If you want to create a random number every month like your topic suggests, use the month as the random seed.
srand(date('n'));
$number = rand();
a very inefficient way would be
<?php
function increm($duration){
while ($i<$duration) {
$i++;
}
return true;
}
$number = 9;
$start = time();
$i = 0;
while (1){
increm(3600*24*30);
$i++;
// Do your code
}
?>
this script would have to be run continuously for months.
A better way would be
<?php
$number = 9;
if(!file_exists('date.txt')){
$date=date('n');
file_put_contents( (string)time());
$date = 0;
}
else{
$date= file_get_contents('date.txt');
$date= date()-(int)$date;
$date= floor($date/(24*3600*30));
}
// do whatever you may
?>
But this script would increase it whenever called as the first open date would be stored. Will work forever (till UNIX can timestamp).
for this purpose you have to store the number in the database, compare with current unix timestamp and update it when the new month is reached.
2 database columns: count_month int(10) and next_month int(10) where next_month will contain the unix timestamp of the first day of the next month. you can run it with cronjobs or on production.
<?php
$now = strtotime("now");
$next_month = strtotime("first day of next month");
if ($query = $dbconnect->prepare("SELECT next_month FROM table1")) {
$query->execute();
$query->bind_result($compare_time);
$query->store_result();
$row_count = $query->num_rows;
if ($row_count > 0) {
while ($query->fetch()) {
if ($compare_time < $now) { // you reached the 1th of the next month time to update
if ($query2 = $dbconnect->prepare("UPDATE table1 SET count_month=count_month +1, next_month=?")) {
$query2->bind_param('i', $next_month);
$query2->execute();
$query2->close();
}
}
}
}
$query->free_result();
$query->close();
}
?>
i have lets say a $value = 5; and the valnue means 5 minutes, and i have a file saved on the server and getting modified a lot called check.txt i want a code to do a calculation of if timenow - timemodification of file <= 0 in H:i:s from the main $value of 5 minutes then continue, else echo please wait minutes left from the time now - filetimemodification of the main value of 5 minutes = $timeleft in m:s format.
i'm testing on the current code but i keep getting a value of -1376352747
my code which is know is bad :) is
$filename = 'check.txt';
$time = date("H:i:s");
$time = str_replace("00", "24", $time);
$filemodtime = filemtime($filename);
$timeleft = $time - $filemodtime;
$h = explode(':', $time);
$h = $h[0];
$h = str_replace("00", "24", $h);
$m = explode(':', $time);
$m = $m[1];
$s = explode(':', $time);
$s = $s[2];
$hms = ("$h:$m:$s");
if (count($filemodtime - $time) <= 0) {
echo "you can continue";
}
else {
echo " please wait $timeleft";
}
thanks in advance
The filemtime() function returns a UNIX-timestamp in seconds, and the time() function returns the current time as a UNIX-timestamp. So by using that difference, you get the file's age in seconds.
$age = time() - filemtime($filename);
// if older then 5 minutes (5 * 60 secounds)
if($age > $value*60)
{
// good
}
else
{
$time_left = $value * 60 - $age;
$time_left_secounds = $time_left % 60;
$time_left_minutes = ($time_left - $time_left_secounds) / 60;
$formated_time_left = sprintf("%02d:%02d", $time_left_minutes, $time_left_secounds);
echo "Please wait {$formated_time_left}";
}
I would recommend to work with time() rather than date().
that way, you can substract the file time from the current time() function, and see if it is bigger than 5 minutes * 60 seconds.
Good luck!
I want to add up time in php but after hours of google'ing and trying out im still unable to find a solution.
my values are:
$newTotal = '00:45:00';
$oldTotal = '00:16:00';
I want to add those two up which make 01:01:00.
Can you give me an example i'm getting really desperate! :p
thanks in advance,
Use strtotime() to turn them into Unix timestamps, then add them as integers:
$newTotal = '00:45:00';
$oldTotal = '00:16:00';
$total = strtotime($newTotal) + strtotime($oldTotal);
To format it as hh:mm:ss again, use date():
echo date('H:i:s', $total);
This gives:
01:01:00
If these values always look like that, you could break them down with a substr()
$hours1 = substr($newTotal, 0, 2);
etc. And then simply add up the seconds, do a divide and mod and bubble up to the hours, and voila!
$secondstotal = $seconds1+$seconds2;
$restseconds = $secondstotal % 60;
$minutesfromseconds = floor($restseconds / 60);
$minutestotal = $minutes1+$minutes2+$minutesfromseconds;
etc.
keep a start date for minimum error.
<?php
$origin = '00:00:00';
$newTotal = '00:45:00';
$oldTotal = '00:16:00';
$added = strtotime($newTotal) + (strtotime($oldTotal) - strtotime($origin));
echo date('H:i:s', $added );
output :
01:01:00
Note, if your time is more than 23:59:59 after adding, you will get wrong result.
Another solution without time function:
function sumtotal($a,$b) {
$i = explode(':',$a);
$j = explode(':',$b); // 0hh:1mm:2ss
$k = array(0,0,0,0); // 0days:1hours:2minutes:3seconds
$k[3] = $i[2]+$j[2];
$k[2] = (int)($k[3]/60)+$i[1]+$j[1];
$k[1] = (int)($k[2]/60)+$i[0]+$j[0];
$k[0] = (int)($k[1]/24);
$k[3] %= 60;
$k[2] %= 60;
$k[1] %= 24;
if ($k[3]<10) $k[3] = '0'.$k[3];
if ($k[2]<10) $k[2] = '0'.$k[2];
if ($k[1]<10) $k[1] = '0'.$k[1];
return $k[0].' days : '.$k[1].' hours : '.$k[2].' minutes : '.$k[3].' seconds';
}
$newTotal = '01:45:21';
$oldTotal = '03:16:56';
echo sumtotal($newTotal,$oldTotal); // result: 0 days : 05 hours : 02 minutes : 17 seconds