convert number of months to the number of years and months - php

I am using a for loop to display numbers 1-60:
for($i=0; $i<=60; $i++)
within the loop, i want to be able to show the number of years and months. for example:
1 month
2 months
3 month
...
1 year
1 year 1 month
1 year 2 month
and so on...
i tried this:
if(!is_float($i/12)) {
$years = $i/12;
} else {
$years = 'no';
}
this shows 1 on the 12 month, 2 on 24 months but not the in between

You can use % and / for the integer part and the rest of division
try this loop for show the result
for($i=1; $i<=60; $i++){
echo 'year = ' . floor($i/12) . ' month = ' .$i%12 . '<br />';
}

I have used the following solution to complete my code with help from #scaisEdge
for($i=0; $i<=60; $i++) {
if(!is_float($i/12)) {
$years = floor($i / 12).' Year';
$years = $years.($years > 1 ? 's' : '');
if($years == 0) {
$years = '';
}
}
$months = ' '.($i % 12).' Month';
if($months == 0 or $months > 1) {
$months = $months.'s';
}
$display = $years.''.$months;
echo '<option value="'.$i.'"';
if($result["warrenty"] == $i) {
echo 'selected="selected"';
}
echo '>'.$display.'</option>';
}

function yearfm($months)
{
$str = '';
if(($y = round(bcdiv($months, 12))))
{
$str .= "$y Year".($y-1 ? 's' : null);
}
if(($m = round($months % 12)))
{
$str .= ($y ? ' ' : null)."$m Month".($m-1 ? 's' : null);
}
return empty($str) ? false : $str;
}
for($x = 0; $x < 100; $x++)
{
var_dump(yearfm($x));
}

another solution 1:
for ($i=0; $i<=60; $i++) {
$output = [];
if ($i >= 12) {
$years = ($i - $i % 12) / 12;
$output[] = $years > 1 ? $years . ' years' : $years . ' year';
}
if ($i % 12 > 0) {
$monthsRest = $i % 12;
$output[] = $monthsRest > 1 ? $monthsRest . ' months' : $monthsRest . ' month';
}
echo implode(' ', $output);
}
another solution 2:
for ($i=0; $i<=60; $i++) {
$output = [];
$startDate = new DateTime();
$endDate = new DateTime('+' . $i . ' months');
$interval = $startDate->diff($endDate);
$years = $interval->format('%y');
$months = $interval->format('%m');
if ($years > 0) {
$output[] = $years > 1 ? $years . ' years' : $years . ' year';
}
if ($months > 0) {
$output[] = $months > 1 ? $months . ' months' : $months . ' month';
}
echo implode(' ', $output);
}

Related

Get number of years, months, days, minutes from number of minutes

How to get the total number of monts, days, and minutes from a given minute. Say for example given a value in minutes 93366 should return 2 months ,5 days and 5 hours. This is what I've tried so far.
function convert_minutes($minutes, $output) {
if ($minutes >= 43833) {
$whole_month = 0;
$decimal_month = 0;
$label = "";
$months = $minutes / 43833;
list($whole_month, $decimal_month) = sscanf($months, '%d.%d');
if ($months > 1) {
$label = "months";
} else {
$label = "month";
}
$output .= $months . " " . $label;
$decimal_month = "0." . $decimal_month;
if ($decimal_month != 0) {
return $this->convert_minutes($decimal_month, $output);
} else {
return $output;
}
} elseif ($minutes >= 1440) {
$whole_day = 0;
$decimal_day = 0;
$label = "";
$days = $minutes / 1440;
list($whole_day, $decimal_day) = sscanf($days, '%d.%d');
if ($days > 1) {
$label = "days";
} else {
$label = "day";
}
$output .= $days . " " . $label;
$decimal_day = "0." . $decimal_day;
if ($decimal_day != 0) {
return $this->convert_minutes($decimal_day, $output);
} else {
return $output;
}
} elseif ($minutes >= 60) {
$whole_minutes = 0;
$decimal_minutes = 0;
$label = "";
$min = $minutes / 60;
list($whole_minutes, $decimal_minutes) = sscanf($min, '%d.%d');
if ($min > 1) {
$label = "minutes";
} else {
$label = "minute";
}
$output .= $min . " " . $label;
$decimal_minutes = "0." . $decimal_minutes;
if ($decimal_minutes != 0) {
return $output . " and " . $decimal_minutes . " minutes";
} else {
return $output;
}
}
}
EDIT
I just wanted to get the estimate. Assuming 1 hour is 60 minutes and 1 day is 1440 minutes and 1 month is 43,200. I am developing a document tracking system and would just like to calculate how long a document stayed in a particular office based on the date received and date released.
You can use floor and mod operator.
Floor rounds down a number.
The modulo operator will give you what is left if split evenly.
Example 5%2 = 1
Since 2*2 = 4 and the remaining is 1.
Echo floor(93366/43200) . " months\n";
$rem = 93366%43200;
Echo floor($rem/1440) . " days\n";
$rem = $rem%1440;
Echo floor($rem/60) . " hours\n";
Echo $rem%60 . " minutes";
Output:
2 months
4 days
20 hours
6 minutes
https://3v4l.org/RreDY

PHP Calendar Issues

I have the following code that I created to generate a Calendar, but it has some issues:
//Labels
$dayLabels = array("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday");
$monthLables = array("January","February","March","April","May","June","July","August","September","October","November","December");
//max values
$maxDays = 7;
$maxMonths = 12;
//stats
$forceMonth = $_GET['m'];
$forceYear = $_GET['y'];
$todayDate = date("d-m-Y");
$todayDate = date("d-m-Y", strtotime($todayDate));
$explodeToday = explode("-", $todayDate);
$currentDay = $explodeToday[0];
if(isset($forceMonth)) {
$currentMonth = $forceMonth;
} else {
$currentMonth = $explodeToday[1];
};
if(isset($forceYear)) {
$currentYear = $forceYear;
} else {
$currentYear = $explodeToday[2];
};
$daysInMonth = cal_days_in_month(CAL_GREGORIAN, $currentMonth, $currentYear);
//database values
$startDate = array("01-06-2015","25-06-2015");
$endDate = array("05-06-2015","05-07-2015");
$bookedUser = array("Dexter","James");
//counters
$daysIntoMonth = 0;
$dayCounter = 0;
//debug
echo '<p>Current Month: ' .$monthLables[$currentMonth-1]. ' / ' .$currentMonth. '</p>';
echo '<p>Current Year: ' .$currentYear. '</p>';
//start of Calendar
echo '<table>';
//print days of week
echo '<tr>';
foreach($dayLabels as $day) {
echo '<td style="border-bottom:dashed 1px #DDD;">' .$day. '</td>';
};
echo '</tr>';
while($daysIntoMonth < $daysInMonth) {
//days into month
$daysIntoMonth++;
$temp_inMonth = sprintf("%02d", $daysIntoMonth);
$daysIntoMonth = $temp_inMonth;
//days into week
$dayCounter++;
$temp_dayCounter = sprintf("%02d", $dayCounter);
$dayCounter = $temp_dayCounter;
//current calendar date
$calDate = date('d-m-Y', strtotime($daysIntoMonth. '-' .$currentMonth. '-' .$currentYear));
$timeCal = strtotime($calDate);
if($dayCounter == 1) {
echo '<tr>';
};
if($startKey = array_search($calDate, $startDate) !== FALSE) {
$booked = true;
};
if($endKey = array_search($calDate, $endDate) !== FALSE) {
$booked = false;
};
if($booked == true) {
echo '<td style="background-color:red;">' .$calDate. ' / ' .$daysIntoMonth. ' ' .$dayCounter. '</td>';
} else if($booked == true && array_search($calDate, $startDate) !== FALSE) {
echo '<td style="background-color:red;">' .$calDate. ' / ' .$daysIntoMonth. ' ' .$dayCounter. '</td>';
} else if($booked == false && array_search($calDate, $endDate) !== FALSE) {
echo '<td style="background-color:red;">' .$calDate. ' / ' .$daysIntoMonth. ' ' .$dayCounter. '</td>';
} else {
echo '<td>' .$calDate. ' / ' .$daysIntoMonth. ' ' .$dayCounter. '</td>';
}
if($dayCounter == $maxDays) {
echo '</tr>';
$dayCounter = 0;
};
};
//table is kill
echo '</table>';
The issues I have noticed:
Unable to put a $bookedUser for respective $startDate,$endDate.
When a booking laps over to another month, it skips all the dates until the $endDate.
All Months start on Monday, how would I go about making them start of correct days of the week.
Possible code examples to help me solve my issues would be great, thanks in advance.
Edit:
I have solved problem 3 by using the following code:
$firstDayofMonth = strtotime("01-$currentMonth-$currentYear");
$firstDayofMonth = date("D", $firstDayofMonth);
$firstDayofMonth = array_search($firstDayofMonth, $dayMiniLabels);
$firstDayofMonth = $firstDayofMonth + 1;
$startMonth = 0;
if($firstDayofMonth != 7) {
while($startMonth < $firstDayofMonth) {
echo '<td></td>';
$startMonth++;
$dayCounter++;
$temp_dayCounter = sprintf("%02d", $dayCounter);
$dayCounter = $temp_dayCounter;
};
};
For the days and months (problem 3), I would do this:
$todaysNumber = date('w');
$currentDayInText = $dayLabels[$todaysNumber];
And the same for the monhts.
Mostly, in my MySQL-tables, dates are placed like 2015-06-05 and not in European time notation. Maybe that could solve problem 1?

how to get date format ( week 3 of month Jan) from week no

I have array of week numbers from 1 to 52. how i can convert it to
[week 1 jan],[week 2 jan] .......
using PHP
OK ... I fix it and here is my code
function getWeeks($date, $rollover)
{
$cut = substr($date, 0, 8);
$daylen = 86400;
$timestamp = strtotime($date);
$first = strtotime($cut . "00");
$elapsed = ($timestamp - $first) / $daylen;
$i = 1;
$weeks = 1;
for($i; $i<=$elapsed; $i++)
{
$dayfind = $cut . (strlen($i) < 2 ? '0' . $i : $i);
$daytimestamp = strtotime($dayfind);
$day = strtolower(date("l", $daytimestamp));
if($day == strtolower($rollover)) $weeks ++;
}
return $weeks;
}
and in the foreach I added
$x="1/1/2013 + ".$record->tms." weeks";
$m=date("Y-m-d", strtotime($x));
$first_week_start=getWeeks($m, "sunday");
if($first_week_start == 1){$typo="st";}
if($first_week_start == 2){$typo="nd";}
if($first_week_start == 3){$typo="rd";}
if($first_week_start == 4){$typo="th";}
if($first_week_start == 5){$typo="th";}
$month=date("M", strtotime($m));
$final_format .= "'".$first_week_start.$typo ." week in ".$month."'";
Try a loop, start with 1/jan, use DateInterval::createFromDateString('1 week'); and DateTime::add each time in the loop to add the next week, use DateTime::format to get the month also checking the current year in each iteration to make sure the loop hasn't moved to the next year.
Code:
date_default_timezone_set("UTC");
$weeks = array();
$dt = new DateTime("2013-01-01");
$interval = DateInterval::createFromDateString("1 week");
for($i=1; $i <= 52; $i++)
{
$weeks[$i] = "week " . $i . " " . $dt->format("M");
$dt = $dt->add($interval);
}
print_r($weeks);
If you want a function that will return a month abbreviation from a week:
function weekMonth($week)
{
date_default_timezone_set("UTC");
return (new DateTime("2013-01-01"))->add(DateInterval::createFromDateString($week." week"))->format("M");
}
echo weekMonth(6);

How to edit the implode so it will join values with two strings?

In the function below a possible output maybe
1 day and 2 hours and 34 minutes
My question is how do I edit the implode so it will output
1 day, 2 houts and 34 minutes
This is my function
function time_difference($endtime){
$hours = (int)date("G",$endtime);
$mins = (int)date("i",$endtime);
// join the values
$diff = implode(' and ', $diff);
if (($hours == 0 ) && ($mins == 0)) {
$diff = "few seconds ago";
}
return $diff;
}
Something like this?
function implodeEx($glue, $pieces, $glueEx = null)
{
if ($glueEx === null)
return implode($glue, $pieces);
$c = count($pieces);
if ($c <= 2)
return implode($glueEx, $pieces);
$lastPiece = array_pop($pieces);
return implode($glue, array_splice($pieces, 0, $c - 1)) . $glueEx . $lastPiece;
}
$a = array('a', 'b', 'c', 'd', 'e');
echo implodeEx(',', $a, ' and ');
I would do something like:
if ($days) {
$diff .= "$days day";
$diff .= $days > 1 ? "s" : "";
}
if ($hours) {
$diff .= $diff ? ", " : "";
$diff .= "$hours hour";
$diff .= $hours > 1 ? "s" : "";
}
if ($mins) {
$diff .= $diff ? " and " : "";
$diff .= "$mins minute";
$diff .= $mins > 1 ? "s" : "";
}
There are a lot of places for x-time-ago functions. here are two in PHP. Here's one in Javascript.

codeigniter timespan function

Hello i have now search the hole web and found a lot but i just dont know how to make it to work so now im asking here for help
i want to do so then a person create a comment it should said "created 1 sec. ago" and then 1 min and 1 hour and like that :)
can some one help me with that ?
thanks
This is basically human readable format, and can be completed by mathematical checks to check the distance of times, working snippet below:
function RelativeTime($timestamp)
{
$difference = time() - $timestamp;
$periods = array("sec", "min", "hour", "day", "week", "month", "years", "decade");
$lengths = array("60","60","24","7","4.35","12","10");
if ($difference > 0)
{
$ending = "ago";
}
else
{
$difference = -$difference;
$ending = "to go";
}
for($j = 0; $difference >= $lengths[$j]; $j++)
{
$difference /= $lengths[$j];
}
$difference = round($difference);
if($difference != 1)
{
$periods[$j].= "s";
}
return $difference . $periods[$j] . $ending;
}
This will do future timestamps such as 12 days to go aswell as timestamps such as 12 days ago
Hope this helps.
Original Source: http://blog.evandavey.com/2008/04/php-date-in-human-readable-form-facebook-style.html
I think this is exactly what you want. When you using the function set $deep parameter to 1.
function timespan($seconds = 1, $time = '', $deep = NULL)
{
$CI = & get_instance();
$CI->lang->load('date');
$current_deep = 0;
if (!is_numeric($seconds))
{
$seconds = 1;
}
if (!is_numeric($time))
{
$time = time();
}
if ($time <= $seconds)
{
$seconds = 1;
}
else
{
$seconds = $time - $seconds;
}
$str = '';
$years = floor($seconds / 31536000);
if ($years > 0)
{
$str .= $years . ' ' . $CI->lang->line((($years > 1) ? 'date_years' : 'date_year')) . ', ';
if (++$current_deep == $deep)
return substr(trim($str), 0, -1);
}
$seconds -= $years * 31536000;
$months = floor($seconds / 2628000);
if ($years > 0 OR $months > 0)
{
if ($months > 0)
{
$str .= $months . ' ' . $CI->lang->line((($months > 1) ? 'date_months' : 'date_month')) . ', ';
if (++$current_deep == $deep)
return substr(trim($str), 0, -1);
}
$seconds -= $months * 2628000;
}
$weeks = floor($seconds / 604800);
if ($years > 0 OR $months > 0 OR $weeks > 0)
{
if ($weeks > 0)
{
$str .= $weeks . ' ' . $CI->lang->line((($weeks > 1) ? 'date_weeks' : 'date_week')) . ', ';
if (++$current_deep == $deep)
return substr(trim($str), 0, -1);
}
$seconds -= $weeks * 604800;
}
$days = floor($seconds / 86400);
if ($months > 0 OR $weeks > 0 OR $days > 0)
{
if ($days > 0)
{
$str .= $days . ' ' . $CI->lang->line((($days > 1) ? 'date_days' : 'date_day')) . ', ';
if (++$current_deep == $deep)
return substr(trim($str), 0, -1);
}
$seconds -= $days * 86400;
}
$hours = floor($seconds / 3600);
if ($days > 0 OR $hours > 0)
{
if ($hours > 0)
{
$str .= $hours . ' ' . $CI->lang->line((($hours > 1) ? 'date_hours' : 'date_hour')) . ', ';
if (++$current_deep == $deep)
return substr(trim($str), 0, -1);
}
$seconds -= $hours * 3600;
}
$minutes = floor($seconds / 60);
if ($days > 0 OR $hours > 0 OR $minutes > 0)
{
if ($minutes > 0)
{
$str .= $minutes . ' ' . $CI->lang->line((($minutes > 1) ? 'date_minutes' : 'date_minute')) . ', ';
if (++$current_deep == $deep)
return substr(trim($str), 0, -1);
}
$seconds -= $minutes * 60;
}
if ($str == '')
{
$str .= $seconds . ' ' . $CI->lang->line((($seconds > 1) ? 'date_seconds' : 'date_second')) . ', ';
}
return substr(trim($str), 0, -1);
}
Source
Assuming you have the difference $now - $creation_time in seconds, a way to do it is to divide it by X seconds (1 minute = 60, 1 hour = 3600, 1 day = 86400) starting with the largest number to see how many of those units fit in your creation time, then use its remainder to try and fit the smaller units in.
$diffSeconds = time() - $creation_time ;
$numDays = $diffSeconds / 86400 ;
$remainderDaySeconds = $diffSeconds % 86400 ;
$numHours = $remainderDaySeconds / 3600 ;
$remainderSeconds = $remainderDaySeconds % 3600 ;
The modulo operator % will give you the remainder of a division. This way, if a post was created less than a day ago then $numDays is 0 and $remainderDaySeconds is $diffSeconds, so you can check and print out accordingly.
Edit I got curious and looked in SO, turns out there are quite a few questions expanding on this. Linking some:
Calculate relative time in C#
calculating and showing a date as 'secs ago', 'mins ago', 'hours ago' etc which points to http://www.php.net/manual/en/function.time.php#89415

Categories