PHP error when using strtotime - php

Hi I get an error when trying to get date interval using php strtotime function
the code is:
<?php
$interval = time() - strtotime('1992/08/13');
//expect to be 18
// but the output is 1988
print date('Y', $interval);
?>
any advice?
thanks

If you want to deal with date intervals in PHP I can't recommend the DateInterval class enough. I wrote a blog post on this earlier this week: Working with Date and Time in PHP
There's an example of using it there that should allow you to do what you want to do.

That is because all time() functions are seconds since epoch which is in 1970, so your out is actually 18 years since epoch. If you want it to get the difference in years you will probably have to calculate the difference yourself.
print $interval / (60*60*24*365.242199);

Are you tring to get the years elapsed rather than the actual year?
If so:
$year = 31556926;
$interval = time() - strtotime('1992/08/13');
print round($interval / $year);

$interval = time() - strtotime('1992/08/13');
These PHP functions deal with UNIX timestamps. That means the number of seconds from 1970. 01. 01. So 1992/08/13 is transformed into a timestamp (seconds). time() gives the current timestamp (seconds). You subtract the former from the latter, and you get the amount of seconds between those two dates. This is not a date itself, just an interval.
If you want to get the year, do something like echo $interval/(60*60*24*365); which will convert your seconds to years (not accurate, leap years will not be taken into consideration). Though your best option is checking out #James C's link and use his solutions. I just wanted to give some explanation.

Related

How to use Carbon PHP to get "hours ago" for a millisecond epoch timestamp?

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/

How to calculate the time difference with two times?

im getting the time with this function :
time();
and im storaging this times in my database,
How can i calculate the difference of current and old date from the database ?
i need results like this : 1 min 10 sec ago
If $time1 and $time2 are both generated by time() ยป UNIX_TIMESTAMP(), you can use:
$diff = $time1 - $time2;
The difference will be in seconds.
Carbon is an exceedingly useful date/time library for PHP. It has a very handy "difference for humans" that will output that sort of difference string. https://github.com/briannesbitt/Carbon#api-humandiff

Check if more than two hours has passed

I'm looking for an easy way to see if more than two hours has passed between two dates. I can either do this with a MySQL DATETIME value, or if needed, I can convert that to a UNIX timestamp. I just need an easy way to to compare those two dates and see if more than 2 hours has passed.
try to look into DATEDIFF function in MySQL.
A UNIX timestamp is just the number of seconds that have elapsed since 12:00AM UTC, January 1, 1970.
Two hours in seconds is 60 * 60 * 2 = 7200. So,
if($secondTimestamp - $firstTimestamp >= 7200)
{
echo '2 hours have elapsed.';
}
Since you tagged with php, you could use PHP's DateTime::diff (DateTime::diff) to get a diff between two datetime objects. I guess it depends on where in your application you are doing the comparison.
In PHP
$time = strtotime($date2) - strtotime($date1); //this will give difference in seconds between two dates
if(($time/3600) >= 2) { // 2 hours has left }

Compare timestamp to date

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);

how to subtract two dates and times to get difference

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?

Categories