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
Related
This question already has answers here:
Return current date plus 7 days
(10 answers)
Closed 2 years ago.
So I have the below property where I'm passing in a time()-86000 value, but what would be the best call to generate a timestamp from the time the class method executes and then adds 7 days to that.
Here is what I got:
$profile->set_picture_expiration(time()-86000)
Would using time()-86000 be the right call? I'd like to write it to the DB in timestamp format from the current time + 7 days.
First of all, if you need +7 days, why are you using minus?
Next, 7 days are 7*24*60*60 = 604800 seconds, not 86000.
Finally, the easiest way to get the timestamp for such relative dates is using the strtotime function. In your specific case it would be strtotime('+7 days').
$profile->set_picture_expiration(strtotime('+7 days'));
How can I get the records from 7 complete days ago?
using Interval 7 days at a time of 1 PM.
For example Select until 7 days ago at 1pm, whereas I need it to be 7 days ago from the start of that day (00:00).
Is there any way within the SQL query to achieve these records or would it require some custom PHP code ?
Use
SELECT DATE_SUB(CURRENT_DATE(), INTERVAL 7 DAY)
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
This question already has answers here:
Timestamp Difference In Hours for PostgreSQL
(9 answers)
Closed 8 years ago.
I am new to PostgresQL and PHP and am working with a PostgresQL Timestamp object and am trying to find the difference between now and that timestamp to display in terms of years, months, and days. Is there to do this?
Thanks!
You can just subtract two timestamps, the result is an interval.
To get "now" you can use now() or current_timestamp (among others).
SELECT now() - '2010-02-21 20:11:32';
This will display something like this, though:
830 days 23:00:50.127241
To get a justified representation, use age() or justify_interval()
SELECT justify_interval(now() - '2010-02-21 20:11:32');
Displays the same value in a format like you seem to be after:
2 years 3 mons 20 days 23:01:34.095813
If you want a particular output format use to_char()
select age(now(), '2010-01-02 12:34:35');
The complete version:
select substring(a from 1 for (position('days' in a) + 3))
from (select (age(now(), '2010-01-02 12:34:35'))::text) s(a)
;
substring
------------------------
2 years 4 mons 30 days
(1 row)
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");