How do operations with dates in cakephp?
I want to calculate the difference in days between two dates, for example: a date subtracting 10 days or a date and add 20 days.
How I do that?
You can use this:
CakeTime::format('r', '+20 days', true);
Take a look on this: http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html
Maybe you wan't see this one too: http://www.php.net/manual/en/function.date.phpenter
Related
I am working in codeigniter. I need to take the difference between two time stamps and use this for link expiry in forgot password service.
So far I have tried.
$date= date('Y-m-d H:i'); this will produce something like 2017-03-20 12:02.
Then again in the near future I want to use the same function to get the currunt time. Eventually measure the difference between these two times in minutes.
$past_time = strtotime('2017-03-20 12:02');
$current_time = time();
$difference = $current_time - $past_time;
$difference_minute = $difference/60;
echo 'Difference in minute between two different Time : '.intval($difference_minute);exit;
You can try with this code. This code for give a difference in minutes between two different Time.
The best way to achieve this is to make an extra column like created_at that contains the datetime of link creation in it. When user click on the link get the current date and time and calculate if the difference is acceptable or not as per your policy.
If you are not using database, then add an extra parameter in your link like created_at or valid_upto with encrypted timestamp in it and use it to calculate the time difference and validate it.
You can save your time in unix timestamp using time() function and use simple subtraction for calculate difference b/w two timestamp. it will return number of second b/w two time then convert second to minute dividing by 60.
OR
you can use strtotime() function to convert date into time stamp.
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.
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
what is the shortest way to calculate the difference in months (average num of days in a month as 30) between two unix timestamps? Date::diff is available for working with DateTime objects, but I'm wondering if there's a neat way to work this out with timestamps...
Well, 30 days are 60*60*24*30=2592000 seconds, so just divide the difference with that number:
(endTime - startTime) / 2592000
I agree with the solution above but it remains inaccurrate. You better use the DateTime object; you can load it with your Unix TimeStamps like this:
$dateTime->setTimestamp( $stamp );
I have a date such as 2009-06-30 (30th june, 2009). I want to calculate a date which appears 2 months, and 3 days before or after the first date. Or, 3 months, 6 days, before or after, etc. How can I do this? Is there an easy way using DATE_SUB() or DATE_ADD()
DATE_ADD(whatever, INTERVAL 2 MONTH) + INTERVAL 3 DAY for example (just to show both of the syntax variants you can use in MySQL for this task) will give a date that's 2 months and 3 days after whatever.
Zend_Date is great for this kind of thing. Zend_Date is part of the Zend_Framework, and you can just use the Zend_Date part, you don't have to use the whole part of the framework.
You can add days or months to a date and it will handle all the complexities for you. It's much easier than any other method I've found in PHP.
You should be able to use DateTime:sub. To get the date 3 months and six days you should be able to use:
date_sub($myDate,"P3M6D");