I have a quickie here,
Whenever I try to echo out the time in hours:min:sec using the date() function everything works perfect.
But when I try echoing it out using a variable with a value, it always adds up 2 hours.
Take a look at the code:
$time = time();
$past = 120;
//this works perfectly
echo $time = date("H:i:s",$time);
//but this doesnt. it adds 2 hours.
echo $time = date("H:i:s",$time);
string date ( string $format [, int $timestamp ] )
On second using of "date" function, second param is string. Like that 01:01:01. But it must be integer. So converting 01:01:01 to integer; it will be "0". What's your purpose?
Watch out what You are doing:
//You are assigning a string to $time variable
echo $time = date("H:i:s",$time);
//second call - trying to format date from unix timestamp, which actually is a string with some hours, minutes and seconds
echo $time = date("H:i:s",$time);
EDIT
Maybe You mean this?
$time = time();
$past = 120;
echo date("H:i:s",$time);
echo date("H:i:s",$time - $past);
Related
I'm having trouble calculating the number of hours worked.
We start with a time which starts as a string in this case ($time).
Then we change the time to 00:00:00 and store the result as a new variable ($newtime).
Then we need to calculate the difference between $time and $newtime but there is a formatting issue which I do not fully understand. Would anyone help?
$time = "2017-09-01 11:00:00"; //must start as a string like this
$newtime = new DateTime($time);
$newtime->setTime(00, 00,00); //change time to 00:00:00
$worktime = round((strtotime($time) - $newtime)/3600, 2);
echo "Hours Worked: " . $worktime . "<br>";
You're subtracting a timestamp with a DateTime object, so it tries to convert the DateTime object to an int, which it can't. You need to get the timestamp for the DateTime object, to subtract two ints:
<?php
$time = "2017-09-01 11:00:00"; //must start as a string like this
$newtime = new DateTime($time);
$newtime->setTime(00, 00,00); //change time to 00:00:00
$worktime = round((strtotime($time) - $newtime->getTimestamp())/3600, 2); // notice the $newtime->getTimestamp() call
echo "Hours Worked: " . $worktime . "<br>";
Demo
DateTime::getTimestamp() reference
You are mixing types (trying to cast object to int)... And maybe you didn't realize about the error you are making because you have disabled errors.
Please use, the method that Datetime class brings to you:
http://php.net/manual/es/datetime.gettimestamp.php
You can do it in both ways:
$newtime->getTimestamp()
or by using this:
date_timestamp_get($newtime)
As this:
$time = "2017-09-01 11:00:00"; //must start as a string like this
$newtime = new DateTime($time);
$newtime->setTime(00, 00,00); //change time to 00:00:00
$worktime = round((strtotime($time) - date_timestamp_get($newtime))/3600, 2);
echo "Hours Worked: " . $worktime . "<br>";
Please, be free of using this: http://sandbox.onlinephpfunctions.com/code/f78c993f709a67ac2770d78bb809e68e3a679707
I have this kind of string:
$duration = "00:20:55.60000";
It was extracted from a video using ffmpeg. I need to convert it to seconds.
I had tried this: strtotime("1970-01-01 $duration UTC"); (I get it from here) but it's not working. May be because it is containing dot at the seconds part.
Is there a way to convert that string to seconds?
Try this:
$duration = "00:20:55.60000";
echo time(strtotime($duration));
// returns : 1485346959
Or
echo date('H:i:s', strtotime($duration));
// returns: 00:20:55
Working Code
just use DateTime class.
$now = '00:20:55.60000';
$timevalue = new DateTime($now);
$onlytime = $timevalue->format('H:i:s');
echo $onlytime ;
$duration = "00:20:55.60000";
echo date('H:i:s', strtotime($duration));
I want to create a function that can find the closest time, based in a string of second.
The system will receive an int number that equivalent of second of that time.
PHP must find the closest (in past) date.
Example:
//supose that an anterior script created it at "14-08-25 10:32:30"
//and now it's "14-08-25 10:33:12"
$seconds = 30; // the variable passed from an anterior script
$time_received= date('Y-m-d H:i:s'); // this is the time that I'll receive this
//so, from these 2 variables above, i must find "14-08-25 10:32:30"
Anyone have an idea how to do this?
I have just these variables:
The time right now, that is "14-08-25 10:33:12"
and the $seconds variable.
With these 2, I want to get "14-08-25 10:32:30"
It isn't completely clear to me what you are trying to accomplish, but I'm making a guess by using strtotime():
<?php
$seconds = 30;
$time = strtotime("-" . $seconds . " seconds");
echo date( "Y-m-d H:i:s", $time );
?>
Found.
I'm using this script:
$seconds = 30;
$new_date = new DateTime(date()); //the only two variables that i have
if($new_date->format('s')<$seconds){
$new_date->setTime($new_date->format('H'),$new_date->format('i')-1,$seconds);
$old_date = $new_date->format('Y/m/d H:i:s');
}else{
$new_date->setTime($new_date->format('H'),$new_date->format('i'),$seconds);
$old_date = $new_date->format('Y/m/d H:i:s');
}
I ran time() at 6:38:47 and it returned a different value than strtotime of the same time. Why is this?
It should absolutely return the same value. Run this code:
<?php
$time = time();
$string = date('Y-m-d H:i:s', $time);
$strtotime = strtotime($string);
print "time = $time\n";
print "string = $string\n";
print "strtotime = $strtotime\n";
print "difference = ".($time-$strtotime)."\n";
?>
My output right now:
time = 1377686839
string = 2013-08-28 12:47:19
strtotime = 1377686839
difference = 0
Are you getting a difference with this? You could also post your test code, maybe there's a mistake in there.
strtotime interprets the time string argument according to the system timezone; time is timezone-independent because it just returns the number of seconds since the start of the Unix epoch.
If your system timezone is anything other than UTC you should expect the values to differ, since the time string argument you passed to strtotime was hardcoded. Your notion of "current wall clock time" and that of the system are different, hence the difference in the timestamps.
I want to get the $registratiedag and count a couple of days extra, but I always get stuck on the fact that it needs to be a UNIX timestamp? I did some google-ing, but I really don't get it.
I hope someone can help me figure this out. This is what I got so far.
$registratiedag = $oUser['UserRegisterDate'];
$today = strtotime('$registratiedag + 6 days');
echo $today;
echo $registratiedag;
echo date('Y-m-d', $today);
There's obviously something wrong with the strtotime('$registratiedag + 6 days'); part, because I always get 1970-01-01
You probably want this:
// Store as a timestamp
$registratiedag = strtotime($oUser['UserRegisterDate']);
$new_date = strtotime('+6 days', $registratiedag);
// You'll need to format for printing $new_date
echo date('Y-m-d', $new_date);
// I think you want to compare $new_date against
// today's date. I'd recommend a string comparison here,
// As time() includes the time as well
// time() is implied as the second argument to date,
// But we'll put it anyways just to be clearer
if( date('Y-m-d', $new_date) == date('Y-m-d', time()) ) {
// The dates are equal, do something here
}
else if($new_date < time()) {
// if the new date is earlier than today
}
// etc.
First it converts $registratiedag to a timestamp, then it adds 6 days
EDIT: You probably should change $today to something less misleading like $modified_date or something
try:
$today = strtorime($registratiedag);
$today += 86400 * 6; // seconds in 1 day * 6 days
at least one of your problems is that PHP does not expand variables in single quotes.
$today = strtotime("$registratiedag + 6 days");
//use double quotes and not single quotes when embedding a php variable in a string
If you want to include the value of variable $registratiedag right into the text passed as parameter of strtotime, you have to enclose that parameter with ", not with '.