Jump to content

How to get the current time and add 10 minutes?


Go to solution Solved by Olumide,

Recommended Posts

  • Solution
7 minutes ago, rwahdan1978 said:

I am trying to get the current time and add 10 minutes before saving to DB.

$addingTenMinutes= strtotime('now() + 10 minute');
$end_time = date('Y-m-d H:i:s', $addingFiveMinutes);

why it is not working?

You're assigning $addingTenMinutes but then using $addingFiveMinutes, which doesn't exist.

Also, strtotime('now() + 10 minute') is not a valid syntax for strtotime. 

 

Here is a corrected version: 

$addingTenMinutes = strtotime('+10 minutes');
$end_time = date('Y-m-d H:i:s', $addingTenMinutes);

 

  • Like 1

strtotime() returns a unix timestamp value.  Timestamps are an integer value which is the number of seconds since the January 1, 1970 00:00:00 UTC.  

Helpers like "+10 minute" are nice for abstraction, but all the minute addition does is add (minutes * 60) to the value.

So you might note that this code will print "Same".

$t1 = strtotime("now") + 10 * 60;
$t2 = strtotime("+10 Minute");

if ($t1 == $t2) {
    echo "Same\n";
}

 

An important limitation of strtotime is that it doesn't have any concept of timezone, so in most cases you should use PHP DateTime classes, which do allow you to account for timezones and translate between them.  You also need to be aware of what the configured locale settings of your server are.  In most cases servers should be setup to be UTC, and thus datetime values set in the database will also be UTC.    This is the best practice.  When you develop your application you want to be aware of the server and PHP settings, and translate the date/time values at presentation time, by applying the desired timezone relevant to your server or the client.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.