SRL Archive - Web Development

PHP math/Date calculation help (function).. pascal guys can probably help


grats

2013-03-24 15:56:53

[PHP] function whatDate($date1) {

$tots = time() - strtotime(str_replace("-","/",$date1));

if($tots>31536000) $tot = round($tots/31536000,0).' year';

else if($tots>2628000) $tot = round($tots/2628000,0).' month';

else if($tots>604800) $tot = round($tots/604800,0).' week';

else if($tots>86400) $tot = round($tots/86400,0).' day';

if($tot>1) $tot .= 's';

return $tot;

}[/PHP]

Alright what I want to do:
I want it to go up to 24 months and then start at 2 years x months // it currently goes to 12 months and then starts 1 year.. and then 2 years, no months

I want it to be like:

1 month
2 months
3 months
…
24 months
2 years
2 years 1 month
2 years 2 months
…
3 years 1 month

etc

can anyone show me how to do that? I am calculating in seconds by the way.. all I am doing is grabbing

2013-3-23 (yesterdays date for example)

and using the exact current time/date to just get how many seconds

I'm just confused on adding multiple things that output there

(how it looks with not bad colors)

xtrapsp

2013-03-24 21:29:17

Could you not give each part a seperate tot name, then return them all as one value?

This might be useful too:

http://php.net/manual/en/datetime.diff.php

grats

2013-03-25 10:41:50

From: xtrapsp
Could you not give each part a seperate tot name, then return them all as one value?

This might be useful too:

http://php.net/manual/en/datetime.diff.php


and google couldn't even find a date difference for php

<?php
$dt1 = date_create('2010-04-01');
$dt2 = date_create(date('Y-m-d'));
$interval = date_diff($dt1,$dt2);
echo $interval->format('%Y year %m months');


?>

am I looking to do something like that being the best way?

get on skype!

revised:
[PHP]<?php

$dt1 = date_create('2010-05-25');
$dt2 = date_create(date('Y-m-d'));
$interval = date_diff($dt1,$dt2);
$mdt = $interval->format('%m');
$ydt = $interval->format('%y');
$dtd = $interval->format('%a');
if($dtd > 730) {
if($ydt > 1) {$ydt = $ydt.' years ';}else if($ydt < 1) {$ydt = '';} else{$ydt = $ydt.' year ';}
if($mdt > 1) {$mdt = $mdt.' months';}else if($mdt < 1) {$mdt = '';} else{$mdt = $mdt.' month';}
$age = $ydt.$mdt.' old';
}else {
if($dtd > 1) {$ms = ' days old';}else {$ms = ' day old';}
if($dtd > 30) {$dtd = round($dtd/30.4166666667, 1);
if($dtd > 1) {$ms = ' months old';}else {$ms = ' month old';}
}
$age = $dtd.$ms;
}
echo $age;

?>[/PHP]
this does exactly what I want, is it the best way to do this?

slushpuppy

2013-03-25 19:16:52

ugh…

what you are trying to do is to create a duration function.


..
$mtime = time() - strtotime($date1);
return duration($mtime);
..


www.hawkee.com/snippet/2056/ <– duration function


strtotime is a string parser that converts any time rep human string into epoch time. Yes there is a rfc standard for it,