In my code, I get the current time and date value of the server following this organization parameter:
Y-m-d H:i:s Ex: 2018-09-01 22:35:18
I would like to remove 30 seconds of the time to use in the if comparison.
How can I accomplish this task?
Use strtotime function to add or subtract time.
If you want to subtract 30 seconds to current timestamp:
$thirtySecondsAgo = date('Y-m-d H:i:s', strtotime('-30seconds'));
If reference date is not NOW:
$date = "2018-08-15 22:51:47";
$dateThirtySecondsAgo = date('Y-m-d H:i:s', strtotime($date . '-30seconds'));
Try this for example current date minus 30 seconds:
$date = date('Y-m-d H:i:s', strtotime('-30 second'));
Related
I am working on my php script to set up the date with the time. I need some help with convert the day date to the current day and next day date, example: my current time is 15:27 and my current date is 27-11-2019 so when I have the string for the variable get_time1 is 06:00:00, I want to convert it to 28-11-2019 06:00:00. When I have the variable get_time2 that have the time which it is 23:00:00 as my current time is before 23:00:00 so i want to convert the date with the current date with the time to 27-11-2019 23:00:00.
Code:
<?php
$get_time1 = '06:00:00';
$get_time2 = '23:00:00';
date_default_timezone_set('Europe/London');
$Date = date('Y-m-d');
$time = date('H:i:s');
?>
Can you please show me an example how I can set up the day date with the time 06:00:00 and 23:00:00 as if the time 06:00:00 is after 12am to set up the next day date and if the time 23:00:00 is before 12am then set up the time with the current date?
Thank you.
This just creates a DateTime object from the time (which will default it to todays date) and if this is less than the current date and time, it adds 1 day...
$date = DateTime::createFromFormat("H:i:s", $get_time2);
if ( $date < new DateTime() ) {
$date->modify("+1 day");
}
which gives
2019-11-27 23:00:00
and for $get_time1...
2019-11-28 06:00:00
If you use a DateTime that will allow you to do date arithmetic.
$now = new DateTime();
$tomorrow = $now->modify("+1 day");
You can also use strtotime to get a unix timestamp as explained in this answer.
$tomorrow = strtotime('+1 day');
maybe this will do?
$offset = timezone_offset_get( timezone_open( "Europe/London" ), new \DateTime() );
echo 'in London' . gmdate('d-m-Y H:i:s', date( "U" )+$offset);
echo 'current location: ' . date('d-m-Y H:i:s', date( "U" ));
I have this time format.
For example: 04d 03h 15m,
when obviously is the time left for something to end or expire. How can I convert that to an actual date and time from now using PHP. As in this example, Say today is 19/1/2017 5:45am, 4 days from now is going to be 23rd Jan 2017, 3:15 am.
Thanks in anticipation.
you can do that by using Date() and time() with strtotime() and Try this code
$currenttime = time();
echo date('d/m/y H:i A', $currenttime);
$timeAfterAdding = strtotime("+4 days +3 hours +15 minutes", $currenttime);
$expireTime = date('d/m/y H:i A', $timeAfterAdding);
echo $expireTime;
output:
current time => 19/01/17 05:12 AM
expire time => 23/01/17 09:27 AM
Given a date string formatted Y-m-d (2014-4-11 for example), how can I get a UNIX timestamp of 12am the beginning of that day? Would it involve date_parse_from_format()?
Thanks
You can simply use strtotime()
$date = "2014-04-11";
$timestamp = strtotime($date);
which inturm gives you -
$d = date('Y-m-d H:i:s', $timestamp); // 2014-04-11 00:00:00
Try online conversion to test - http://www.onlineconversion.com/unix_time.htm
I can't figure out how to add 10 minutes to a time I am getting from mysql database the field structure is datetime.
Current code
$nowtime = date('Y-m-d h:i:s');
$timeoflastlogin = $db->gettime();
//ADD 10 MINS TO TIME LAST ATTEMPTED
$endtime = strtotime('+ 10 minutes', $timeoflastlogin );
//$endtime = date('Y-m-d h:i:s', strtotime('+ 10 minutes', $timeoflastlogin );
This displays "
2011-09-07 13:53:43 < time of last login
2011-09-07 03:56:15 < now time
2611< endtime - this is supposed to be time of last +10 mins "
I cannot work out how to add 10 mins to the time/date from mysql, I need to do this and then set a command to compare the time now and of last login so if it has been 10 minutes I can stop the user trying to login again!
Thanks for your help
$timeoflastlogin is not a Unix timestamp but a string - so you can't use that as a $now parameter for strtotime. This should work:
$endtime = strtotime('+ 10 minutes', strtotime( $timeoflastlogin ) );
or easier:
$endtime = strtotime( $timeoflastlogin ) + 600; // 10 minutes == 600 seconds
You can do it directly in the database:
SELECT DATE_ADD(datefield, INTERVAL 10 MINUTE)
FROM ...
This saves you the PHP overhead of having to re-parse the date string into a time value, do the addition, then re-convert to a string.
Have a look at http://www.php.net/manual/en/datetime.createfromformat.php and http://www.php.net/manual/en/datetime.add.php.
Here is how I have done it in the past:
Use php's mktime function:
http://www.php.net/manual/en/function.mktime.php
This lets you pass in values for day, month, year, etc (which you can pull out of your sql time). If the minutes is greater than 60, it will automatically adjust the hours for you. It will then return a time stamp.
To get this timestamp back into a sql date format, use the date function.
Something kind of like this:
echo date("M-d-Y", mktime($hour, $min + 10, $sec, $month, $day, $year));
You will have to adjust the "M-d-Y" to get the format you want.
I get the time:
$today = time();
$date = date('h:i:s A', strtotime($today));
if the current time is "1:00:00 am", how do i add 10 more hours to become 11:00:00 am??
strtotime() gives you a number back that represents a time in seconds. To increment it, add the corresponding number of seconds you want to add. 10 hours = 60*60*10 = 36000, so...
$date = date('h:i:s A', strtotime($today)+36000); // $today is today date
Edit: I had assumed you had a string time in $today - if you're just using the current time, even simpler:
$date = date('h:i:s A', time()+36000); // time() returns a time in seconds already
$tz = new DateTimeZone('Europe/London');
$date = new DateTime($today, $tz);
$date->modify('+10 hours');
// use $date->format() to outputs the result.
see DateTime Class
(PHP 5 >= 5.2.0)
You can simply make use of the DateTime class , OOP Style.
<?php
$date = new DateTime('1:00:00');
$date->add(new DateInterval('PT10H'));
echo $date->format('H:i:s a'); //"prints" 11:00:00 a.m
$date = date('h:i:s A', strtotime($today . ' + 10 hours'));
(untested)
$date = date('h:i:s A', strtotime($today . " +10 hours"));
Full code that shows now and 10 minutes added.....
$nowtime = date("Y-m-d H:i:s");
echo $nowtime;
$date = date('Y-m-d H:i:s', strtotime($nowtime . ' + 10 minute'));
echo "<br>".$date;
In order to increase or decrease time using strtotime you could use a Relative format in the first argument.
In your case to increase the current time by 10 hours:
$date = date('h:i:s A', strtotime('+10 hours'));
In case you need to apply the change to another timestamp, the second argument can be specified.
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.
So, the recommended way since PHP 5.3:
$dt = new DateTime(); // assuming we need to add to the current time
$dt->add(new DateInterval('PT10H'));
$date = $dt->format('h:i:s A');
or using aliases:
$dt = date_create(); // assuming we need to add to the current time
date_add($dt, date_interval_create_from_date_string('10 hours'));
$date = date_format($dt, 'h:i:s A');
In all cases the default time zone will be used unless a time zone is specified.