Am using formula like this
<?php
//Coming From Mobile Phone
$Currenttime = 12:40:00;
//Server time Is Current Time of various timezone like(America/Chicago,America/Denver..)
$ServerTime = 12:55:00;
//Scheduletime is coming from database
$ScheduleTime = 14:45:00;
$ExactTime = ($Currenttime - $ServerTime) + $ScheduleTime
?>
I need Exact Time is like this 15:00:00
You should use the DateTime class for this:
$Currenttime = DateTime::createFromFormat('H:i:s', "12:40:00");
$ServerTime = DateTime::createFromFormat('H:i:s', "12:55:00");
$ScheduleTime = DateTime::createFromFormat('H:i:s', "14:45:00");
$ExactTime = $ScheduleTime->add($Currenttime->diff($ServerTime));
Related
I'm working on a tick based space game http://ricarion.com/ but the ticks only run between certain hours.
08:00-16:30 - run every 30 minutes via a cron job. In the nav bar at the top I want to add "Next Tick: 08:30 06/02/20" for example.
So I was thinking of creating an array:
$tick_times[] = array();
$tick_times[] = 08:00;
$tick_times[] = 08:30;
$tick_times[] = 09:00;
...
$tick_times[] = 16:30;
And then this is where I get stuck, how do I check the existing time, and then compare that against the array selecting the next future time? i.e. It's now 08:34, so the return should be 09:00?
Did you need an array or just want to calculate the next 30-minute interval?
If so this may be similar to:
Round minute down to nearest quarter hour
You do modulo division of 1800 seconds on the current time and add the answer (time remainder of time to the next interval) to the then-current time to get the next event.
<?php
$current_date = date('d-M-Y g:i:s A');
echo $current_date."\n";
$current_time = strtotime($current_date);
$frac = 1800;
$r = $current_time % $frac;
$new_time = $current_time + ($frac-$r);
$new_date = date('d-M-Y g:i:s A', $new_time);
echo $new_date."\n";
http://codepad.org/xs9lMCRQ
Get the now time format it and compare it. In your case you maybe format your $tick_time to the same format like current time.
$date = new DateTime('now');
$date = $date->format('Y-m-d H:i:s');
foreach ($tick_times as $tick_time) {
$date_added = new DateTime($tick_time);
if (strtotime($date_added) == strtotime($date)) {
//do your stuff here
}
}
i use ths method to find the difference between two timestamp and get the number of seconds between those two times, and i refresh the information with jquery like a counter.
$diff = strtotime(date('Y-m-d H:i:s')) - strtotime('2014-06-25 14:50:03');
$time = intval(date('s', $diff));
echo $time;
When the difference is more than 60 seconds, the $time comes back to 0, like a reset.
i would like to display 1 min XX s for example
The s flag for date() will never return a value greater than 59 as it only represents the current number of seconds of a given time which can never be more than 59 before rolling over into a new minute.
If you want the total number of seconds you can actually remove your second line of code as the difference between two Unix Timestamps is always in seconds:
$time = strtotime(date('Y-m-d H:i:s')) - strtotime('2014-06-25 14:50:03');
echo $time;
If you want to display this as minutes and seconds you can use DateTime() which offers better tools for this:
$now = new DateTime();
$then = new DateTime('2014-06-25 14:50:03');
$diff = $now->diff($then);
echo $diff->format('%i minutes %s seconds');
format the date
$diff = strtotime(date('Y-m-d H:i:s')) - strtotime('2014-06-25 14:50:03');
$time = date('i:s', $diff);
echo $time;
Pass time like 1 & now 2
function diffrencePassTimeAction($DataTime){
$im = $DataTime - strtotime("now");
return $im;
}
Future time like 2 & now 1
function diffrenceFuturTimeAction($DataTime){
$im = strtotime("now") - $DataTime;
return $im;
}
this function delete (-less)
function diffrencePassTimeAction($DataTime){
if ($DataTime > 0)
return $DataTime - strtotime("now");
else
return strtotime("now"); // OR return 0;
}
Hello I try to take the difference between two dates and display it.
My problem is that the time difference I get is not the correct one.
This is my code:
$time1 = strtotime('2014-03-28 15:20:00');
$time2 = strtotime('2014-03-28 15:15:00');
$diffTime = $time1 - $time2;
echo date('H:i', $diffTime);
The result I get is:
02:05
The currect time should be this:
00:05
My guess that the date somehow takes timezone or something like this but Im not sure.
Thanks.
/****************************************
$start_date = new DateTime('23:58:40'); *These two still give
$end_date = new DateTime('00:00:00'); *a wrong answer
*****************************************/
$start_date = new DateTime('23:58:40');
$end_date = new DateTime('00:11:36');
$dd = date_diff($end_date, $start_date);
//Giving a wrong answer: Hours = 23, Minutes = 47, Seconds = 4
echo "Hours = $dd->h, Minutes = $dd->i, Seconds = $dd->s";
So what you're actually doing here is generating two UNIX timestamps (numbers) and then subtracting them. then you're passing the resulting number as if it were still a timestamp to date().
essentially $diffTime is the number of seconds between your two times. you could divide by 60 to get minutes, and so on and so forth, but PHPs DateTime objects are much better.
From the PHP docs:
http://pl1.php.net/strtotime
Note:
Using this function for mathematical operations is not advisable. It is better to use DateTime::add() and DateTime::sub() in PHP 5.3 and later, or DateTime::modify() in PHP 5.2.
try this
<?php
$time1 = strtotime('2014-03-28 15:20:00');
$time2 = strtotime('2014-03-28 15:15:00');
echo round(abs($time1 - $time2) / 60,2). " minute"
?>
Below is the solution of date time in years,days.hours,minutes and seconds.
$time1 = strtotime('2014-03-28 15:20:00');
$time2 = strtotime('2014-03-28 15:15:00');
$diffTime = $time1 - $time2;
$y = ($diffTime/(60*60*24*365));
$d = ($diffTime/(60*60*24))%365;
$h = ($diffTime/(60*60))%24;
$m = ($diffTime/60)%60;
$s = ($diffTime)%60;
echo "Minutes - " .$m;
echo "<br/>";
I'm having some trouble calculating if time is past 15 minutes, on a date and time in my database. How can I check this?
I have this in my database (example):
2014-01-30 15:29:31
And then I get the date from PHP:
$date = date("Y-m-d H:i:s");
But how can I check if the time has passed 15 minutes, or if it's a different day?
I have this code so far:
$date = date("Y-m-d H:i:s");
$sql = "SELECT date FROM reset WHERE session_id = '".$sessid."' limit 1";
$result = mysql_query($sql);
$value = mysql_fetch_object($result);
$dbdate = $value->date;
$checkdate = strtotime($dbdate);
if ($checkdate - time() > 15 * 60) {
error_log("15 mins passed");
}
You can convert the date to a timestamp with strtotime (which supports the MySQL date format) and then compare it to the current timestamp from time.
$dbtimestamp = strtotime($datefromdb);
if (time() - $dbtimestamp > 15 * 60) {
// 15 mins has passed
}
To compare the dates, you can use date to get the year/month/day from the timestamp and then compare them against the current date.
if (date("Y-m-d", $dbtimestamp) != date("Y-m-d")) {
// different date
}
Using a DateTime object:
$dateTimeObject = new \DateTime($dateString);
//and subtract using an interval
$dateTimeObject->sub(new \DateInterval("PT15M"));
$newDateString = $dateTimeObject->format("Y-m-d H:i:s");
you cans use DATE_FORMAT functions.
$actual_minute = date("i");
$data = mysql_query("SELECT DATE_FORMAT(date,'%i') minutes FROM reset WHERE session_id = '".$sessid."'");
$database_minute = $data{'minutes'};
I am having table time in mysql database with one attribute of type "TIME" which contains default value "09:00:00". what I am trying to do is to get this value and subtract it from current time.
include 'connection.php';
$time = mysql_query("SELECT start_time FROM time");
$s = mysql_fetch_assoc($time);
$start_time = strtotime($s['start_time']);
$time_now = date("H:i:s");
$delay = ($time_now - $start_time);
However it never worked the way I need. result always like 00:00:00
what i want to achieve is something like:
$start_time = 09:00:00
$time_now = 09:34:23
so $delay should be 00:34:23.
any help to achieve that?
Thanks in advance.
Just a small bug in your code.... look below
include 'connection.php';
$time = mysql_query("SELECT start_time FROM time");
$s = mysql_fetch_assoc($time);
$start_time = strtotime($s['start_time']);
$time_now = date("H:i:s");
$delay = ($time_now - $start_time); //BUG! String minus timestamp here...
Fix that second block with:
$start_time = strtotime($s['start_time']);
$delay = date( "H:i:s", time() - $start_time );
Agree with others though, this is really cleanly done on the database side as well.
Hope this helps