I'm a novice to php's date() and strtotime, and have been attempting to find out the solution to this for the great portion of the day with no real solution (I've come close, but to no avail).
What I have is a typical database row with a 'submitted' column, which is entered via a submitted=NOW() (in datetime format). I'm attempting to get the current datetime and find the difference between both values in "x Hours and x minutes". To make matters a little more interesting my web server is an hour behind me in terms of timezones. I've tried the "date_default_timezone_set('EST');" and it does help doing the straight date() function but obviously doesn't help me with my already inserted datetimes.
$lastEntryDate = date('l, F dS Y', strtotime($entryDate));
$lastEntryTime = date('g:ia', strtotime($entryDate.'+1 hour'));
$currDate = date('l, F dS Y');
$currTime = date('g:ia');
So, tried doing $lastEntryTime - $currTime, but that obviously gets messed up depending on the time of day (as it's in 24 hour format, I believe).
I've googled around and found a couple of posts on forums indicating using the 3600 (seconds in an hour), and I'm still trying to wrap my head around this.
Is there something basic I'm missing? Or is this quite complex as I think it is?
$now = time();
$entrytime = strtotime($entryDate) + (60 * 60) //60 seconds times 60 minutes = 1 hour
$difference = $now - $entrytime;
$hours = floor($difference / (60 * 60));
$minutes = $difference - ($hours * 60 * 60);
a solution that may be helpful to you is instead of using the MySQL now function why not make your submitted field of the INT type and then store a unix timestamp in it using the php time() function, then before inserting that into the database just add 1 hour in seconds to the timestamp. Then when you retrieve the data from your table you should be able to work with the timestamp and the php date and time function to create whatever timestamp you want. Here is a link on the php time() function which may help:
http://php.net/manual/en/function.time.php
Related
I have tried this, where $time = 1409065068000.
$ago = Carbon::createFromTimeStamp($time)->diffInHours();
That time stamp is from somewhere in August 26th. But my code returns me:
391015508
It shouldn't be like this. The hours ago should rather be 48 or something.
diff implies you're trying to get a different between two values, but the only value you've supplied is your timestamp. So Carbon's probably going against your time v.s. the epoch:
1409065068000 / 1000 = 1409065068 seconds
1409065068 / 60 / 60 = 391406963 hours
If you'd read the docs: https://github.com/briannesbitt/Carbon#api-difference you'd see that the various diff functions take in another carbon object that you want to diff against.
Carbon::createFromTimestamp takes epoch timestamp in seconds as input. If you want to input epoch timestamp in milliseconds then you can use Carbon::createFromTimestampMs
$ago = Carbon::createFromTimeStampMs($time)->diffInHours();
This will return the correct value.
Following code may solve your problem-
$time = $dt->timestamp(1409065068000)->timezone('Europe/London');
echo Carbon::now()->diffForHumans(Carbon::now()->subYear($time));
Try for this code, hope it will work.
However, it is highly recommended to take a look following tutorials-
https://github.com/briannesbitt/Carbon
http://tisuchi.com/php-date-time-customization-carbon/
Is there a simple straight forward way to find the remaining time between a start date - ex: 2013-01-10 12:34:55 and 5 minutes later?
What I mean is I have the start date and want to check that 5 or 60 minutes later gives a time difference of 0. Kind of a time out to be checked on server side.
You have 2 dates, correct ?
$date1 = strtotime('2013-01-10 12:34:33'); // converto to time
$date2 = strtotime('2013-01-10 12:45:33'); // or else it won't work
$diff = date('u', $date1) - date('u', $date2); // the difference in seconds between the two
// if you want $date2 to be now, just use date('u')
if ($diff > 3600) { // an hour later
echo 'The difference between $date1 and $date2 is more than an hour';
}
If you want to do something like "session time out" for the user. Use Javascript setintervel() and do ajax() call to logout user from the application.
Well, seems like I was going the harder way... found that it would be easier and more reliable to get it from an sql check than in php.
I've implemented this with success. Leave it here just in case it might be useful.
$query = "SELECT * FROM db_table
WHERE DATE_ADD(my_start_date, INTERVAL 5 MINUTE) > NOW()";
Where 5 is the interval that can be set in seconds (SECOND), minutes (MINUTE), hours (HOUR), days (DAY) etc.
I have a field in my DB that records the last time a check occured, I also have frequency of how often the checks should occur.
What I need is: I get the past timestamp and I need to add the frequency in minutes to it to check against current time stamp.
Can't think of a way via PHP, anyone could help?
#fredley commented with just the right answer:
timestamp += minutes * 60
If you still want a plain PHP variant of that why not try strtotime?
//$oldTimestamp;
$minutes = 15;
$newTimestamp = strtotime('+ ' . $minutes . ' minutes', $oldTimestamp);
Depending on your database configuration, and table layout, you may want to look at the MySQL ADDTIME function
I need to compare a timestamp to a date. I would just like to compare the date portion without the time bit. I need to check whether a timestamp occurs on the day before yesterday i.e. today - 2.
Could you show me a snippet please? Thank you.
I've been reading through the PHP docs but couldn't find a very clean way of doing this. What I found was converting the timestamp to a date with a particular format and comparing it to a date which I get by doing a time delta to get the date before yesterday and converting it to a particular format. Messy.
You can arcieve this by using the function strtotime.
To round to a day I personaly like to edit the timestamp. This is a notations of seconds since epoch. One day is 86400 seconds, so if you do the following caculation:
$time = $time - ( $time % 86400 );
You can convert it back to a date again with the date function of PHP, for example:
$readableFormat = date( 'd-m-Y', $time );
There is also much on the internet about this topic.
you can use the strtotime function
<?php
$time = strtotime("5 june 2010");
$before = strtotime("-1 day",$time);
$after = strtotime("+1 day",$time);
i have to sent an email when a user register email contain a link that is become invalid after six hours
what i m doing when email is sent i update the db with field emailSentDate of type "datetime"
now i got the curent date and time and has made to the same formate as it is in db now i want to find that both these dates and time have differenc of 6 hours or not so that i can make link invalid but i donot know how to do this
my code is look like this i m using hardcoded value for db just for example
$current_date_time=date("Y-m-d h:i:s");
$current=explode(" ",$current_date_time);
$current_date=$current[0];
$current_time=$current[1];
$db_date_time="2010-07-30 13:11:50";
$db=explode(" ",$db_date_time);
$db_date=$db[0];
$db_time=$db[1];
i do not know how to proceed plz help
<?php
//$now = new DateTime(); // current date/time
$now = new DateTime("2010-07-28 01:11:50");
$ref = new DateTime("2010-07-30 05:56:40");
$diff = $now->diff($ref);
printf('%d days, %d hours, %d minutes', $diff->d, $diff->h, $diff->i);
prints 2 days, 4 hours, 44 minutes
see http://docs.php.net/datetime.diff
edit: But you could also shift the problem more to the database side, e.g. by storing the expiration date/time in the table and then do a query like
... WHERE key='7gedufgweufg' AND expires<Now()
Many rdbms have reasonable/good support for date/time arithmetic.
What you can do is convert both of your dates to Unix epoch times, that is, the equivalent number of seconds since midnight on the 31st of December 1969. From that you can easily deduce the amount of time elapsed between the two dates. To do this you can either use mktime() or strtotime()
All the best.
$hoursDiff = ( time() - strtotime("2010-07-30 13:11:50") )/(60 * 60);
I'd rather work with a timestamp: Save the value which is returned by "time()" as "savedTime" to your database (that's a timestamp in seconds). Subtract that number from "time()" when you check for your six hours.
if ((time() - savedTime) > 6 * 3600)
// more than 6h ago
or
"SELECT FROM table WHERE savedTime < " . (time() - 6 * 3600)
This might be the solution to your problem -> How to calculate the difference between two dates using PHP?