I want to do something like this
strtotime("next starting minute");
If the current time is 15:23:21, i want to get 15:24:00
Any ideas?
Don't use strtotime() but use time() and ceil().
$next_minute_timestamp = ceil(time()/60)*60;
This way, easy calculated you will always get the timestamp for the next minute. Because I divide the timestamp by 60, it will return the number of minutes. Ceiling this will get you the next minute. Then the timestamp again is multiplied by 60 to get the seconds, which is the timestamp you need.
$timestamp = mktime(date('H'), date('i') + 1, 0);
strtotime(date('H') . ":" . (date('i') + 1) . ":00");
Get the time, subtract the seconds, add 60 seconds for one minute:
$timestamp = time()-(time()%60)+60;
my solution , it worked for me.
$nexminute = date("H:i", time()+60).":00";
Related
I'm trying to run a query where a check is done if a time is in between a time and that time + 90 mins.
I have 1 time value being passed in. So lets say the time being passed in is 1pm. I'd like to check if a time is between 1pm and 2.30pm.
I have tried passing in a second param adding the seconds with strtotime() and I have the below
AND r.start_time BETWEEN '19:00:00.0000' AND '19:00:00.0000', INTERVAL + 90 MINUTE
I think you can use strtotime(...)+5400 to express 90 minutes.
For example:
$times = strtotime('19:00:00.0000');
$end_time = $times + 5400;
$query = "SELECT ... WHERE r.start_time BETWEEN '$times' AND '$end_times'";
So I need to convert an unix timestamp which is in seconds to milliseconds.
This line seems to be not working
$unixtime = strtotime($timestamp_conv."+3 hour") * 1000;
I basically need a 13 digit timestamp.
Any ideas what I'm doing wrong?
Thanks
As per the comments, $timestamp_conv already is a (second-)timestamp, you want to convert to a (milliseconds-)timestamp. You, however, also try to add some offset (3 hours) to it.
With simple arithmetics this would look like this
// add the three hours: 3 hours of 60 minutes of 60 seconds each
$timestamp_conv += 3 * 60 * 60;
// convert to milliseconds base
$unixtime = $timestamp_conv * 1000;
You can use DateTime from php, it's more intuitive.
<?php
$t = new \DateTime();
$t->setTimestamp(1492498242);
$t->modify("+3 hours");
echo $t->getTimestamp()*1000;
Hope it helps!
May i ask you how can i calculate the period by deduct two time parameter like below
$time_1 = $rows['code_send_time'];
$period = time() - $time_1;
after I echo the $period how can I exchange to minute and know how many minutes has passed?
The result after I deduct is "151341193030" and I want the result will be "30" (mins) or "1800" (seconds)
Thanks for your help!
You can use the DateTime object for this. If you check out the docs there is this method: DateTime::diff
http://snipplr.com/view/1459/sec2hms/
Add what you have to this:
sec2hms( $period );
$time_1 = $rows['code_send_time'];
// As you said you get 151341193030 here
$period = time() - $time_1;
$period contains the number of seconds. So if you want to get minutes you can divide it by 60 and it you'd like to get hours then divide it by 3600.
I'm using date('H:i:s', (time() - $start['time'])) to displa time that passed since start of the script.
whatever the result of time() - $start['time'] - be it 0, 17 or whatever the date prints out like 02:00:00, 02:00:17 etc.
What may be the reason?
time returns an absolute numeric timestamp, say the numeric value for 2012-06-04 16:35:12. Your start time is a similar numeric, absolute timestamp. Subtracting one from the other will result in a very small number, which is, again, an absolute timestamp. Likely some time around the beginning of 1970. When you format that timestamp using date('H:i:s'), you only display the time portion of a timestamp like 1970-01-01 02:00:00.
Read about what UNIX timestamps actually represent.
The time difference that you're looking for is the result of time() - $start['time'], which is in seconds, which you can't simply format using date(). More along the lines of:
$diff = time() - $start['time'];
echo 'Difference in seconds: ' . $diff;
echo 'Difference in minutes: ' . $diff / 60;
echo 'Difference in hours: ' . $diff / 60 / 60;
seems you have a 2H offset added.
// save current TZ
$current_tz = date_default_timezone_get();
// Use universal time
date_default_timezone_set('UTC');
$t = time();
echo "T:".$t."<br/>";
sleep(2);
echo "T2: ".date('H:i:s', time() - $t );
// return correct TZ
date_default_timezone_set($current_tz);
If i try this code without the date_default_timezone_set('UTC') it gives me expected value + 1H from Summer Time (GMT+1). If you only need the diference between the 2 times you can use UTC for both and you only get the diference OR you can check the current time offset and subtract it.
Hope it helps
I am pulling a datetime from a mysql db and i would like to add X hours to it then compare it to the current time. So far i got
$dateNow = strtotime(date('Y-m-d H:i:s'));
$dbTime = strtotime($row[0]);
then i tried $dbTime + strtotime("4 hours"); but 4 hours seem to add 4hrs to the current time instead of raw 4hours. How do i add X hours to dbTime?
NOTE: I am using php 5.1.2 so date_add doesnt work (5.3.0)
You have quite a few options here:
1.
$result = mysql_query("SELECT myDate FROM table");
$myDate = mysql_result($result, 0);
$fourHoursAhead = strtotime("+4 hours", strtotime($myDate));
2.
// same first two lines from above
$fourHoursAhead = strtotime($myDate) + 4 * 60 * 60;
3.
$result = mysql_query("SELECT UNIX_TIMESTAMP(myDate) FROM table");
$myDate = mysql_result($result, 0);
$fourHoursAhead = $myDate + 4 * 60 * 60;
4.
$fourHoursAhead = strtotime("+4 hours", $myDate);
5.
$result = mysql_query("SELECT UNIX_TIMESTAMP(DATE_ADD(myDate, INTERVAL 4 HOUR))");
$fourHoursAhead = mysql_result($result, 0);
then i tried $dbTime + strtotime("4 hours"); but 4 hours seem to add 4hrs to the current time instead of raw 4hours. How do i add X hours to dbTime?
strtotime has an optional second argument. Provide a Unix timestamp there and the output will be relative to that date instead of the current date.
$newTime = strtotime('+4 hours', $dbTime);
You can also use the fact that Unix timestamps are seconds-based - if you know what four hours are in seconds, you can just add that to the time integer value.
time() and strtotime() result in unix timestamps in seconds, so you can do something like the following, provided your db and do your comparison:
$fourHours = 60 * 60 * 4;
$futureTime = time() + $fourHours;
strtotime("+4 hours", $dbTime);
The second argument is the timestamp which is used as a base for the calculation of relative dates; it defaults to the current time. Check out the documentation.
Edit:
For short periods of time, max 1 week, adding seconds to a timestamp is perfectly acceptable. There is always (7 * 24 * 3600) seconds in a week; the same cannot be said for a month or year. Furthermore, a unix timestamp is just the number of seconds that have elapsed since the Unix Epoch (January 1 1970 00:00:00 GMT). That is not effected by timezones or daylight-savings. Timezones and daylight-savings are only important when converting a unix timestamp to an actual calendar day and time.
I tend to use the time() function, and this page from the manual shows them displaying the date a week in the future:
http://us3.php.net/manual/en/function.time.php
Here's how I'd do it:
Pull the time from the database using the UNIX_TIMESTAMP() function.
The UNIX timestamp is in seconds, so add 4*60*60 to it.
Convert the modified UNIX timestamp to a date using PHP's localtime() or strftime() function.
query("SELECT UNIX_TIMESTAMP(someDatetimeColumn) ...");
. . .
$dbTimeAdjusted = localtime($row[0] + 4*60*60);
Probably the safest way to do the compare is right in the SQL
SELECT * FROM my_table WHERE someDateTimeColumn < DATE_ADD(NOW(), INTERVAL 4 hour)
And since you're assembling it in PHP, you can dynamically replace the "4 hour" bit with whatever your code needs to compare.
(Note: putting the entire calculation on the other side of the comparison to the column allows MySQL to do the calculation once per query, rather than once per row, and also use the table's index, if that column has one.)
Assuming that the timestamp returned by the DB is in SQL format, the following should work fine:
$dbTime = strtotime($row[0]);
$nowTime = time();
$future_dbTime = strtotime("+4 hours", $dbTime);
$diff_time_seconds = $nowTime - $dbTime;
if ($diff_time_seconds > 0) {
echo "The current time is greater than the database time by:\n";
$not_equal = true;
}
if ($diff_time_seconds == 0) {
echo "The current time is equal to the database time!";
}
if ($diff_time_seconds < 0) {
echo "The current time is less than the database time by:\n";
$not_equal = true;
}
if ($not_equal) {
$diff_time_abs_seconds = abs($diff_time_seconds);
echo date('h:m:s', $diff_time_abs_seconds);
}