Hey guys i'm trying to figure out how to subtract one time from another using php to get the amount of time left between the two times. So for example
time left = time1-time2
or
timeleft = 15:35-15:30
which would be equal to 5mins left.
Currently I am loading the two times like so.
time1 is coming from my database (which is the time we are waiting for, and in my case the time we are waiting for is the time for next update) and time2 is the current system time.
I tried using this code
$timeleft = $dbtime - $curtime;
$dbtime = time loaded from database.
$curtime = current system time.
But that just returns a 0.
Any help is appreciated thanks.
Use strtotime to turn the date string to unix timestamp.
$timeleft = strtotime($dbtime) - strtotime($curtime);
You have to convert both times into timestamp. One good function for that is the strtotime() http://php.net/manual/en/function.strtotime.php that try to convert a string into timestamp.
Then do your maths as you know and then just use the date() http://www.php.net/manual/en/function.date.php function to fomrat your time into anything you like
Use strtotime to convert strings to unix timestamps:
$timedifference = strtotime($dbtime) - strtotime($curtime); // or also
$timedifference = strtotime($dbtime) - time();
You negating one string from another - the result in 0 because (int)(string) = 0
Your must use like this
$dbtime = time();
// query
$timeleft = time() - $dbtime;
See also strtotime() function, if you date is parsed by it
Related
I only have basic PHP knowledge and I'm using PHP+Mysql and trying to check the difference in days between 2 dates; the 1st date is formatted by myself in the script as a string:
$f_entrega=$_POST['year1']."-".$_POST['month1']."-".$_POST['day1'];
The second date ($f_dock) which is the one causing the issue is taken from the mysql database which column is in DATE format. To compare the dates I do the following:
if(!empty($_POST["id"])){
$f_entrega=$_POST['f_entrega_a']."-".$_POST['f_entrega_m']."-".$_POST['f_entrega_d'];
$f_entr=$f_entrega;
$mysqli=conectar();
$resultado = $mysqli->query("SELECT id,f_dock FROM pt");
$row = $resultado->fetch_assoc();
for ($i=0;$i<count($ids);$i++){
do{
if ($ids[$i]==$row["id"]){
$f_dock=$row["f_dock"];
break;
}
} while ($row = $resultado->fetch_assoc());
$error=0;
var_dump($f_dock);
$f_dock=strtotime($f_dock);
$f_dock=date('Ymd',$f_dock);
$f_entrega=$f_entr;
$f_entrega=strtotime($f_entrega);
$f_entrega=date('Ymd',$f_entrega);
$f_dock=DateTime::createFromFormat('Ymd',$f_dock);
$f_entrega=DateTime::createFromFormat('Ymd',$f_entrega);
$dias_vendor=date_diff($f_dock,$f_entrega);
$tat=$dias_vendor->format('%R%a');
Sometimes it works correctly, but other times I get Warning: strtotime() expects parameter 1 to be string, object given in [first line] and $tat is not correctly calculated and has strange values.
I've tried different solutions like $f_dock=(string)$f_dock before but finally the convertion always fails in some cases. Thanks in advance for any tip.
The error that you are getting is because the string you are entering is not a valid string for the strtotime() function to convert.
For instance 2015-08-31 will convert just fine, as will today, tomorrow or +7 days.
For more specific help you will need to tell us what the value of $f_dock is (as Marcos says in his comment, var_dump($f_dock) will get you this).
However, on to the solution:
$date1 = strtotime($f_dock); //timestamp in seconds
$date2 = strtotime($f_entrega); //same for the second date
$difference = $date1 - $date2; //difference in seconds between the dates
$days = floor($difference/86400);
86400 is the number of seconds in a day, so find out how many seconds difference there is, then see how many days worth of seconds are in there and use floor() to round the number down. Job done.
I have tried this, where $time = 1409065068000.
$ago = Carbon::createFromTimeStamp($time)->diffInHours();
That time stamp is from somewhere in August 26th. But my code returns me:
391015508
It shouldn't be like this. The hours ago should rather be 48 or something.
diff implies you're trying to get a different between two values, but the only value you've supplied is your timestamp. So Carbon's probably going against your time v.s. the epoch:
1409065068000 / 1000 = 1409065068 seconds
1409065068 / 60 / 60 = 391406963 hours
If you'd read the docs: https://github.com/briannesbitt/Carbon#api-difference you'd see that the various diff functions take in another carbon object that you want to diff against.
Carbon::createFromTimestamp takes epoch timestamp in seconds as input. If you want to input epoch timestamp in milliseconds then you can use Carbon::createFromTimestampMs
$ago = Carbon::createFromTimeStampMs($time)->diffInHours();
This will return the correct value.
Following code may solve your problem-
$time = $dt->timestamp(1409065068000)->timezone('Europe/London');
echo Carbon::now()->diffForHumans(Carbon::now()->subYear($time));
Try for this code, hope it will work.
However, it is highly recommended to take a look following tutorials-
https://github.com/briannesbitt/Carbon
http://tisuchi.com/php-date-time-customization-carbon/
With PHP, I am trying to convert a bunch of numbers into a a readable format, the thing is, I have no idea how/what format these are in or can be parsed in using the date() or time() functions in php. there are two of these as well.
(they're built from a total time spent online and time since last log-on)
onlinetime : 1544946 = 2w 3d 21h 9m
lastonline : 1397087222 = 1h 32m
does anyone know the way to get the two different times from the two different timestamps?
If you have a Unix timestamp, take a look at Convert timestamp to readable date/time PHP. The PHP documentation is here: http://php.net/manual/en/function.date.php.
For the online time, you could do modulo arithmetic to figure out the values for each, and then just make a string out of the result. Someone may have a nicer suggestion for this though.
I think John is right, the first is the number of seconds in the timespan listed. And the second certainly looks like a unix timestamp to me. So here's how you can get what you want from these sets of numbers:
1) For the first number, simply divide the number by the seconds in a given time span and use floor():
$timeElapsed = 154496; // in this case
$weeksElapsed = floor($timeElapsed / 604800);
$remainder = $timeElapsed % 604800;
$daysElapsed = floor($remainder / 86400);
etc...
2) For the second number, you can do the same thing by first getting the current timestamp and then subtracting the given timestamp from it:
$lastOnline = 1397087222; // again, in this case
$currentTimestamp = time();
$elapsedSinceLastLogin = $currentTimestamp - $lastonline;
$weeksSinceLastLogin = floor($elapsedSinceLastLogin / 604800);
etc...
I'd like my user to be able to input values like:
4 hours
23 minutes
etc.
strtotime works great for converting these values into seconds, but it adds them to the current time. Is there a way of getting it to return the quantity of time entered in total, rather than from now? Or do I need to do something like this:
$time = strtotime($value) - time();
And just for arguments sake, what would happen if the value of time changes between strtotime evaluating it and time evaluating it?
$time = strtotime($value, 0);
http://php.net/strtotime
$time = strtotime($value) - time();
And just for arguments sake, what would happen if the value of time changes between strtotime evaluating it and time evaluating it?
Then you will miss some seconds (probably 1), because strtotime($value) is evaluated first and if the time goes by the result of time() will be bigger then expected (1 second probably ;))
You can make sure you're always working with the same time using this pattern:
$start = time();
$time = strtotime($value, $start) - $start;
Or even easier, skip the subtraction by setting the second argument to 0:
$time = strtotime($time, 0);
I am trying to create a javascript countdown timer;
I have a string that is in the format of YYYY-MM-DD HH:MM:SS .
This could be any time up to 6 months in the future.
What would be the best way to go about getting the time remaining in seconds from now until the future time. This could be implemented in PHP.
Thanks in advance!
In PHP you can use strtotime, which takes a string representation of a date and returns the unix timestamp.
Then use microtime to get the current unix timestamp, and find the difference. This will be the number of milliseconds remaining, so divide it by 1000 to get it in seconds.
http://php.net/manual/en/function.strtotime.php
http://php.net/manual/en/function.microtime.php
This should work:
$currentTime = explode(" ", microtime());
$currentTime = $currentTime[1];
$futureTime = strtotime("YYYY-MM-DD HH:MM:SS"); // insert your date here
$timeRemaining = ($futureTime - $currentTime) / 1000;
How are you getting this string-based timestamp? A unix timestamp is actually already "number of seconds since 1970-01-01 00:00:00". That looks like a native MySQL date string.
If it is coming out of MySQL, you can convert it to a unix-style timestamp with UNIX_TIMESTAMP(), e.g.
SELECT unix_timestamp(datetimefield) ...
and then convert it to a Javascript timestamp by multiplying by 1000 (JS timestamps have the same epoch, but in milliseconds).
If you're stuck in PHP, you can go quick/dirt with
$timestamp = strtotime($time_string);
$js_timestamp = $timestamp * 1000;