Hello I'm trying to check how long message is in database I'm using laravel4 framework and I have this:
date( "h", strtotime($message->created_at)) - date('h')
but it only counts hours I need to change days and month to hours and then count how long is it in the database. How can i do it?
date() returns a string, and you can't do math on strings. date('h') returns the current hour, eg: 10 now because it is 10am. If $message->created_at was Feb 3, 1978 10:01:02 then date('h', strtotime($message->created_at)) would return 10 as well.
Assuming $message->created_at is in an acceptable format:
$diff_in_hours = (time() - strtotime($message->created_at)) / 3600;
Your question is a bit unclear, but it sounds to me like you're looking for what is called a "human readable timestamp". If you've set up your database table correctly, both created_at and updated_at should be retrieved as carbon objects, giving you several different ways to display your date. It's actually very simple to achieve the desired effect:
$message->created_at->diffForHumans()
This should then print a readable time just like you see here on stackoverflow (1 hour ago, 23 minutes ago, etc...).
Check out the link for more information about carbon objects.
Related
I was wondering if there is a way to update a certain number value, per year?
Similar to how you would update a website date in the footer like this:
<?php echo date("Y"); ?>
I'm looking for a way to update a set number each year / month.
Example, say a website says they have been in business for 10 years. Then when the next year passes, I want PHP to automatically update the value to 11 years. Note, that I have to start with the number 10
Another example would be for age: John is 21. After a year passes I want PHP to update the site to say, John is 22.
Or Betty has worked here for 5 months.... After the next month passes it would update to 6, then once it hits 12 months is would change to 1 year then 2 years etc... Thats a little more involved, but you get the idea.
If someone knows how this can be achieved I would appreciate the help, or if you can point me in the right direction to solving this problem that would work too.
Yes, you need to understand two things - the current date and the date you are comparing it to. You then simply need to compare the too.
I would suggest using the PHP DateTime, DateInterval, etc. classes which greatly simply this for you.
For example:
$now = new DateTime('now');
$comparison_date = new DateTime('2005-01-01'); // roughly 10 years ago
$interval = $now->diff($comparison_date); // returns DateInterval object
$years_difference = $interval->y;
You can find your answer here:
How to calculate the difference between two dates using PHP?
By using strtotime() to convert two dates to unix time and calculate the number of seconds between them then reconvert to human readable.
To calculate the difference between to years you can use a simple sum like this:
<?php
$cur_year = date("Y");
$year = 2008;
$total_years = $cur_year - $year;
echo ($total_years);
?>
For months you could use a similar thing
Substract the initial Unix timestamp with the current timestamp (time() returns it), divide by 60 * 60 * 24 * 365, and round accordingly (you will probably want to floor() the years an user has been registered, and ceil() the years a bussiness have been working)
$start = 12345646;
$years = ceil((time() - $start) / 31536000.0); // Important: use a float
Note this uses "year" as a set of 365 days. It won't take into account leap years.
I am experiencing a rather strange problem using PHP 5.3's date diff function to calculate the difference in days between two dates. Below is my code:
$currentDate = new DateTime(); // (today's date is 2012-1-27)
$startDate = new DateTime('2012-04-01');
$diff = $startDate->diff($currentDate);
$daysBefore = $diff->d;
echo $daysBefore;
The above code displays 4 as the value of the $daysBefore variable.
Why is PHP displaying a difference of 4 days between the dates 27th Jan 2012 and 1st April 2012, when clearly there are many more days between these dates.
Am I doing something wrong?
DateInterval::$d is the days part of the interval, not the total number of days of the difference. For that, you want DateInterval::$days, so:
$daysBefore = $diff->days;
When creating a DateInterval through the DateTime::diff method, it populates not just days, but hours, minutes, seconds, months and even years in the single character properties. You're checking single-character d for days, which will be the days left over once years and months are calculated.
Try looking at the days property, which only actually gets populated when you use diff.
Behavior here is wildly inconsistent. Check out the DateInterval::format manual page for some interesting information about what happens when you create a DateInterval through various means.
The d property is the number of days as in "3 months, 4 days". If you want the total number of days, use the days property.
4 days, and a couple months...
Use $diff->days for total number of days.
http://www.php.net/manual/en/class.dateinterval.php
This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
Datatype/structure to store timezones in MySQL
I'll be storing only UTC times in my database, and want to convert the timestamps to a user's local time using PHP.
My question is, what is the best way to store the users timezone in the database?
IE: -400, -4, -4:00
With that said, I know how to do the math manually:
$utc_time = strtotime("2011-10-02 23:00:00");
$my_time = $utc_time - 14400;
echo date($timeformat, $mytime);
My problem is, with the user's timezone being pulled from the database, I don't know whether I'll be adding or subtracting seconds. That's where I run into the problem of trying to figure out how to save the timezone so that I can calculate the offset.
I can save the timezone as "-14400", but using the above example, I can't just combine the two strings to do the math for me using the - sign:
$my_time = $utc_time.$timezone;
So... how do I save the timezone properly to get the math done?
The timezone in one location isn't always the same - for example, the UK is BST (GMT + 1) between March and October. Use one of the timezones supported by PHP:
http://php.net/manual/en/timezones.php
If you do go ahead using numbers, either store them as hours or minutes. Store timezones west of UTC/GMT as negative numbers. For example, the US East Coast would be -5 (hours) or -300 (minutes) - assuming it's 5 hours behind.
Then, add this to the timestamp - the negative or positive will handle the rest.
// for 5 hours behind when stored as hours (-5)
$now = time() + ($offset * 60 * 60);
// for 5 hours behind when stored as minutes (-300)
$now = time() + ($offset * 60);
timezone string would be better than the offset or difference in hours
because some timezones are subject of changes
http://www.php.net/manual/en/class.datetime.php
http://www.php.net/manual/en/class.datetimezone.php
for date/time math:
http://www.php.net/manual/en/class.dateinterval.php
http://www.php.net/manual/en/class.dateperiod.php
stop using php functions, oop classes are much better!
Rather than storing it as a string, store it as a number? Then you can simply add that number to the timestamp. (The number can be negative for timezones which would subtract.)
I am converting PHP (v 5.2.17)-based reports to SSRS.
I'm trying to write a query that subtracts dates like the php file does.
The date calculation either matches exactly, or it differs by exactly 3600 seconds
$timediff=strtotime(date("Y-m-d"))-strtotime("03/29/2007");
print $timediff; // 137635200
select (trunc(sysdate) - to_date('03/29/2007','MM/DD/YYYY'))*60*60*24 from dual
-- returns 137635200 - matches
$timediff=strtotime(date("Y-m-d"))-strtotime("11/23/2009");
print $timediff; // 53823600
select (trunc(sysdate) - to_date('11/23/2009','MM/DD/YYYY'))*60*60*24 from dual
-- returns 53827200 - doesnt match - off by 3600
I've searched stackoverflow and found the following example which sounds like it (11/23/2009 is a monday, and so is today 8/8, but it doesnt differ in the same way) PHP Strtotime erratic function
My assumption is that the php calculation is wrong, and the oracle is correct.
What say you? ;-)
Thanks!
Without seeing the exact values I suspect its because one of them is adjusting for daylight savings and the other is not. Most parts of the world change to/from daylight savings time during march. So in your first example both dates are in the same GMT offset, but in the second (November) they're not.
So one of your platforms (probably Oracle) is taking this one hour shift into account and the other is not.
3600 = an hour so could be timezone differences?
53827200 = ( 623 * 60*60*24 )
So 53827200 is exactly 623 days of 24 hours.
53823600 is 622.958333 days or 622 days (of 24 hours) plus 23 hours.
Both are right, for a given definition of right.
Let's say I have a datetime, June 16 2011 at 7:00. I want to be able to check at, say, August 5 2011 at 7:00 and be able to tell that it is exactly a multiple of 1 day since the first date, whereas 7:01 would not count, since it is not an exact multiple.
Another test set: Let's say we have June 16 2011 at 7:00, and I want to check if a particular minute is within an interval of exactly 2 hours since then. So 9:00, 11:00, 13:00, etc. would count, but 9:30 and 10:00 would not. And this could continue for days and months - September 1 at 7:00 would still count as within every 2 hours. (And no, at the moment I don't know how I'm going to handle DST :D)
I thought about it for a moment and couldn't think of anything already existing in PHP or MySQL to do this easily but hell, it could, so I wanted to throw this up and ask before I start reinventing the wheel.
This is on PHP 5.1, sadly.
select *
from test
where datetimefield > '2011-06-16 07:00:00'
and
mod(timestampdiff(second,'2011-06-16 07:00:00',datetimefield),7200) = 0
This example will give you all the records greater than '2011-06-16 07:00:00' where the field is exactly a multiple of 2 hours.
Easiest would be to convert the date/time values into a unix timestamp and then simply do some subtraction/division:
2011-06-16 07:00:00 -> 1308229200
2011-08-05 07:00:00 -> 1312549200
2011-08-05 07:00:01 -> 1312549201
1312549200 - 1308229200 = 4320000 / 86400 = 50 (days)
1312549201 - 1308229200 = 4320001 / 86400 = 50.0000115...
So in other words:
if (($end_timestamp - $start_timestamp) % 864000)) == 0) {
... even multiple ...
}
Same would hold for the day/week comparisons. For months, this'll be out the window, since months aren't nice even figures to deal with.
MySQL Date functions:
http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html
You can use TIME() to get just the time part of a date. If the time parts are the same it is an exact multiple.
For the two hour thing, one way to do it would be to get the minute/seconds part of the date, make sure those are equal, then make sure that the hour parts of the dates are both even or both odd. For more complicated integer (e.g. 5) hour multiples, you can "fake" doing a mod by dividing the hour parts and checking if the result is an int.
You can compare two DateTime objects via diff() method. Result is a DateInterval object - you can check the exact number of days/hours/minutes between two dates.
It's useless to write your own algorithms if you can use built-in functionality.