I'm basically trying to add 24 hours to a date with php and display it but it keeps adding only 23 hours in stead of 24 hours.
<?php
$create_time = strtotime('2015-03-18 20:03:23');
$set_time = $create_time + 3600*24;
echo gmdate("Y-m-d H:i:s", $set_time);
?>
So the result that I'm getting out of this is:
2015-03-19 19:03:23
but it's this what should be coming out of this:
2015-03-19 20:03:23
I'm new at working with these time functions and I can't figure out why it keeps getting adding 23 hours. Obviously I can multiply it by 25 and get 24 hours but that doesn't make sense to me.
So my question is: what's the proper way to add 24 hours to a date?
I would do it like so:
date("Y-m-d H:i:s", strtotime("+1 day"));
strtotime() uses default time zone, gmdate() uses Greenwich Mean Time (GMT). Try using date() instead.
<?php
$create_time = strtotime('2015-03-18 20:03:23');
$set_time = $create_time + 3600*24;
echo date("Y-m-d H:i:s", $set_time);
?>
Related
I am writing a login system in PHP and would like a to implement a function that checks if the 15 minutes has elapsed since the last failed login attempt.
In the MySQL database in my users table there is a column called last_login_datetime which is defined as SQL's DATETIME format, ex. 2018-02-24 21:12:57.
This is the code I am using to check if 15 minutes has elapsed since the last login fail
$dateTime = date("Y-m-d H:i:s");
if($row['user_locked']==$statusY AND $dateTime - strtotime($row['last_loginfail_datetime'] > 15 * 60))
{
.....
}
This does not seem to work for me at the moment and was hoping if someone could point out where I am going wrong?
Thanks in advance.
It doesn't work because you are substracting unix time from a string date. What you need to do is to get $dateTime as unix time.
$dateTime = strtotime("now");
Just use strtotime($dateTime) instead of $dateTime in the if condition. strtotime returns a UNIX tiemstamp i.e: the number of seconds since January 1 1970 00:00:00 GMT
$dateTime = date("Y-m-d H:i:s");
if($row['user_locked']==$statusY AND strtotime($dateTime) - strtotime($row['last_loginfail_datetime'] > 15 * 60))
{
.....
}
Your code won't work because your date will be read as a string. Try this instead:
$dateTime = strtotime("now");
if($row['user_locked']==$statusY AND $dateTime - strtotime($row['last_loginfail_datetime'] > 15 * 60))
{
.....
}
I would like to check if a time in an SQL database is more than a day old.
I am able to read the datetime off the SQL row and get the current time. What I am unable to do, however, is add 1 day/24 hours to the time pulled from the row, and comparing the 2.
$currenttime = date("Y-m-d h:m:s");
$tmstp = $row['datetime'];
$newtime = date("Y-m-d h:m:s", $tmstp);
echo $tmstp."<br>".$currenttime."<br>".$newtime;
returns the following:
Notice: A non well formed numeric value encountered in C:\xampp\htdocs\test.php on line 12
0000-00-00 00:00:00
2016-03-12 09:03:39
1970-01-01 01:01:00
the null time is to be expected, I initialized the row but didn't pass a value to it. Setting it to anything other than null(including setting it to an arbitrary date more than 24 hours ago but after 1970-1-1 OR adding one hour to it:
$currenttime = date("Y-m-d h:m:s");
$tmstp = $row['datetime'] + strtotime('+ 24 hours');
$newtime = date("Y-m-d h:m:s", $tmstp);
echo $tmstp."<br>".$currenttime."<br>".$newtime;
sets it to tomorrow at this time:
1457903185
2016-03-12 10:03:25
2016-03-13 10:03:25
Once I can get the expected values, how would I effectively compare the 2 values?
Thanks for any help
For compare times you can use the strtotime like this:
$tomorrow = strtotime('+ 24 hours');
$timestamp = strtotime($row['datetime']);
if ($timestamp > $tomorrow) {
// The date in database is greater than tomorrow
}
I am having trouble adding a time and time interval to a date.
I use a javascript datepicker that gives date in the form: 05/06/2013
I can convert this into a date time format with
$eventdate = date("Y-m-d H:i:s",strtotime($eventdate));
This gives: 2013-05-06 00:00:00
I get the time from a separate variable in minutes. Eight o'clock would be 480.
However, I cannot seem to find syntax to add 480 minutes to the date.
Have tried, for example,
echo date("Y/m/d h:i:s", strtotime("+480 minutes", $eventdate));
but this gives me the default 1969 date.
Thanks for any help or suggestions!
Use the DateTime class, it's easier to work with:
$eventdate = \DateTime::createFromFormat('Y/m/d h:i:s', $eventdate);
$eventdate->modify('+480 minutes');
echo $eventdate->format('Y/m/d h:i:s');
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Given a time, how can I find the time one month ago
How can I print an hour ago in PHP using Date?
$date=date("Y-m-d H:i:s");
$time(-1, now);
$result=$date.$time;
So If I wanted to say "John visited last "
Would print
John visited last 20th Feb 2012, 17.26
$date = date('Y-m-d H:i:s', strtotime('-1 hour'));
echo 'John visited last ' . $date;
$date = date("Y-m-d H:i:s", time() - 3600);
time() -> Current timestamp
Time minus 3600 seconds, is the time 1 hour ago. To get the date formatted, you can look here for the options: http://php.net/manual/en/function.date.php
Alternatively you could use the following format:
$date = date("Y-m-d H:i:s", strtotime('-1 hour'));
Though using that method can be a little clunky if you want to remove more specific units of time (Such as one day and 3 hours).
Thats if I've understood what you want to do correctly that is.
I suppose you would be fetching date and time from mysql and the best thing to do is using mysql's DATE_FORMAT function and work out.
Other wise in simple php you could do it like this
$date=date("Y-m-d H:i:s", $time -3600);
Better option is to use strtotime like this one
$date=date("Y-m-d H:i:s", strtotime('-1 hour'));
And get the work done.
Mmm, search the manual the function I used. You are missing something about PHP date/time functions...
// Get the date string for time() - 3600, that is
// the current time minus 3600 seconds (= 1 hour)
$date = date("Y-m-d H:i:s", time() - 3600);
$result = $date;
Simple question but this is killing my time.
Any simple solution to add 30 minutes to current time in php with GMT+8?
I think one of the best solutions and easiest is:
date("Y-m-d", strtotime("+30 minutes"))
Maybe it's not the most efficient but is one of the more understandable.
This is an old question that seems answered, but as someone pointed out above, if you use the DateTime class and PHP < 5.3.0, you can't use the add method, but you can use modify:
$date = new DateTime();
$date->modify("+30 minutes"); //or whatever value you want
Time 30 minutes later
$newTime = date("Y-m-d H:i:s",strtotime(date("Y-m-d H:i:s")." +30 minutes"))
$timeIn30Minutes = mktime(idate("H"), idate("i") + 30);
or
$timeIn30Minutes = time() + 30*60; // 30 minutes * 60 seconds/minute
The result will be a UNIX timestamp of the current time plus 30 minutes.
echo $date = date('H:i:s', strtotime('13:00:00 + 30 minutes') );
13:00:00 - any inputted time
30 minutes - any interval you wish (20 hours, 10 minutes, 1 seconds etc...)
It looks like you are after the DateTime function add - use it like this:
$date = new DateTime();
date_add($date, new DateInterval("PT30M"));
(Note: untested, but according to the docs, it should work)
$dateTime = new DateTime('now', new DateTimeZone('Asia/Kolkata'));
echo $dateTime->modify("+10 minutes")->format("H:i:s A");
$ck=2016-09-13 14:12:33;
$endtime = date('H-i-s', strtotime("+05 minutes", strtotime($ck)));
In addition to Khriz's answer.
If you need to add 5 minutes to the current time in Mysql format you can do:
$cur_time=date("Y-m-d H:i:s");
$duration='+5 minutes';
echo date('Y-m-d H:i:s', strtotime($duration, strtotime($cur_time)));
time after 30 min, this easiest solution in php
date('Y-m-d H:i:s', strtotime("+30 minutes"));
for DateTime class (PHP 5 >= 5.2.0, PHP 7)
$dateobj = new DateTime();
$dateobj ->modify("+30 minutes");
The question is a little old, but I come back to it often ;p
Another way, which is also a one liner:
<?= date_create('2111-11-11 00:00:00')->modify("+30 minutes")->format('Y-m-d h:i:s') ?>
Or from timestamp, returns Y-m-d h:i:s:
<?= date_create('#'.time())->modify("+30 minutes")->format('Y-m-d h:i:s') ?>
Or from timestamp, returns timestamp:
<?= date_create('#'.time())->modify("+30 minutes")->format('U') ?>
new DateTime('+30minutes')
As simple as the accepted solution but gives you a DateTime object instead of a Unix timestamp.
$time = strtotime(date('2016-02-03 12:00:00'));
echo date("H:i:s",strtotime("-30 minutes", $time));