Output format while building mysql php countdown function [duplicate] - php

This question already has answers here:
Converting timestamp to time ago in PHP e.g 1 day ago, 2 days ago...
(32 answers)
Closed 8 years ago.
I've been struggling with this for quite sometime, after few long hours spend on forums I came up with this code:
SELECT *, TIMESTAMPDIFF(SECOND,NOW(),`pay_date`) AS `expire` FROM `users`
pay_date is datetime field which in the moment of tries had setup (current time + 7 days)
so NOW() is showing 03-09-2013 23:30:20; pay_date is showing 10-09-2013 23:30:20. I'am using this code to extract my countdown:
echo $date['expire'];
It is working, its properly giving me amount of seconds left, what i dont know is how to make it to say something like:
2 years, 10 months, 20 days, 5 hours, 30 minutes, 46 seconds left.
ive tried in few ways for instance date('d-m-Y H:i:s', $date['expire']) but its giving me as output something like 1790-01-01, strtotime isnt also working, i dont know how to make it to work in the way i described above.

You can use DateTime class for time calculation:
Code:
$start = new DateTime;
$end = clone $start;
$end->modify("+{$date['expire']} seconds");
$diff = $start->diff($end);
print_r($diff);
Output:
DateInterval Object
(
[y] => 0
[m] => 0
[d] => 0
[h] => 0
[i] => 4
[s] => 29
[invert] => 0
[days] => 0
)
As you can see in output, you have all the info you need. To access it, just use $diff->i for minutes, $diff->s for seconds etc. or use DateInterval::format for formating interval.

I strongly suggest using PHP's DateTime object instead of strtotime.
http://www.php.net/manual/en/book.datetime.php
You can get the date difference using Datetime::diff method.
Also, you can style output however you want:
$datetime->diff()->format('%y Years, %m Months, %d Days')

sorry guys but none of these works, this stuff time_elapsed_string($ptime) no matter what i do, i can even put there date manually and it still returns 44 years ...
new so cold feature DateTime class isnt accurate, its so far from true while calculating, there is more professional and working solution for this, i will wait until someone present solution that actually works.
Again, i believe that nobody wants amateur function that sometimes works sometimes not and is badly written (months always 30 days ? no thanks). While reading php manual you will qucikly notice that there is a lot of proof's that datetime class isnt working, even on stackoverflow ive seen that kind of posts.
This stuff is good:
select *,Concat(hour(diffTime),' hours ',minute(diffTime),' Minutes ',second(diffTime),' seconds remaining') as timetaken
from (select *,sec_to_time(UNIX_TIMESTAMP(now())- UNIX_TIMESTAMP( ttable.pay_date))
as diffTime from users ttable )
as temptable1
i've found it on different topic also related to date calculating, adapted it to my database and it works, but unfortunetely it shows only Hours Minutes and Seconds, when its like few days it shows 800 hours for example, i dont understand how it works exactly, but i believe it could be modified to show also years months and days perhaps someone will know how to do it

Related

Carbon date difference in minutes, hours, days, etc

I am using the Carbon Date library.
I want the difference between two dates which outputs directly as below:
If the difference is in minutes, then it should show something like 26 Minutes
If the difference in hours, then it should show something like 4 hours 3 Minutes
If the difference is in days, hours, minutes then it should show something like 2 Days 9 Hours 7 Minutes
If the difference is in days & minutes, then it should show something like 3 Days 10 Minutes and NOT 3 Days 0 Hours 10 Minutes
I am having a solution where I check, d, h, i and set the data manually, also I don't want the solution as 0 Days 0 Hours 15 Minutes.
The logic should be smart enough to omit the 0 ones.
So I searched under Carbon Documentation, but I didn't find one, I am not looking for the difference for Humans.
Hence I am looking if there is a Carbon Method which I have not discovered.
Thanks.

Convert Postgres interval to PHP DateInterval

I am saving a value into a Postgres10 interval type field.
When I return the field, I get the following: 3 years 10 mons 1 day 02:18:00
The problem I seem to be having, mostly, is that it would also return 1 day 02:18:00 if the duration is not years and months long.
Is there a clean way to convert this value into a PHP DateInterval object?
I figure I can use regex but that seems a little messy to me.
If you change the interval output style to iso_8601 using
SET intervalstyle = 'iso_8601';
You will get output in the correct format for a call to DateInterval::__construct e.g.
SELECT
INTERVAL '6 years 5 months 4 days 3 hours 2 minutes 1 second';
Output:
interval
P6Y5M4DT3H2M1S
Demo on dbfiddle

PHP DateTime rounding months properly for missing day numbers? [duplicate]

This question already has answers here:
PHP DateTime::modify adding and subtracting months
(19 answers)
Closed 6 years ago.
This is the first time I've ever used the built-in DateTime class, but we have to do calculations based on months previous/forward and are getting some interesting results when inputting day numbers that exist in one month but not another, and I'm not sure if PHP is really doing it accurately! (BTW, I've only tested this on v5.2 legacy code which I have to work on for now)
So for instance, if I input today's date (2016-10-31), subtract 6 months (with ->modify('-6 months')), the date outputted (with ->format('Y-m-d')) is May 1st! (2016-05-01). This implies PHP is just moving up the chain to the days in the next month (so 29 for Feb in a non-leap year is Mar 1, 30 is Mar 2, 31 is Mar 3, etc).
Using this logic, I deduced May 31 minus 1 month would be May 1st, which it was when I tested it!
Is this an accurate way to add/subtract by month? I'm not sure yet if our departments calculate this way, but I'm curious if anyone else has run into this.
This is a bit broad but...
The problem with how you're thinking is that
2016-10-31 - 6 months = 2016-04-30
PHP is thinking as such
$date = new DateTime('2016-10-31');
$date->modify('-6 months'); // PHP subtracts 183 days in all the tests I ran
echo $date->format('Y-m-d'); // 2016-05-01
You're going to have to come up with your own methodology because modify is basically a guesstimate for things like months, which can be 28-31 days long. In other words, PHP does a lot of things that are "good enough" if you don't need high precision.

How to change date to hours

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.

Strange PHP 5.3 issue with date diff calculating difference in days

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

Categories