1

I'm trying to build a website with a calendar converted from Gregorian to IFC. The conversion is simple enough, but I'm not savvy enough in PHP to do all the calculations to display today's date.

I've managed to print current month from a working variable, $dayofyear, which counts the days since Jan 1st (manual input), and then displaying current month through this:

    $now = time(); // or your date as well
    $your_date = strtotime("2020-01-01"); //I'd also love to get this replaced with a string automatically starting from jan 1st of current year so I don't have to update it every year.
    $dayofyear = $now - $your_date;
    $day = round($dayofyear / (60 * 60 * 24));
    $date = round($dayofyear / (7));

    if ($day > 0 && $day < 27) {
        echo 'January';
    }
    elseif ($day > 28 && $day < 55) {
        echo 'February';
    }
    elseif ($day > 56 && $day < 83) {
        echo 'March';
    }
    elseif ($day > 84 && $day < 111) {
        echo 'April';
    }
    elseif ($day > 112 && $day < 139) {
    echo 'May';
    }
    elseif ($day > 140 && $day < 168) {
        echo 'June';
    }
    elseif ($day > 169 && $day < 196) {
        echo 'Sol';
    }
    elseif ($day > 197 && $day < 224) {
        echo 'July';
    }
    elseif ($day > 225 && $day < 252) {
        echo 'August';
    }
    elseif ($day > 253 && $day < 280) {
        echo 'September';
    }
    elseif ($day > 281 && $day < 308) {
        echo 'October';
    }
    elseif ($day > 309 && $day < 336) {
        echo 'November';
    }
    elseif ($day > 337 && $day < 365) {
        echo 'December';
    }

    else {
        echo '<i>error...</i>';
    }

Also, alternatives to this, to make the code shorter would be greatly appreciated. I've been trying to achieve this through javascript, but unsuccessfully...

Dani Dale
  • 11
  • 1
  • Why not just use the date() function? https://www.php.net/manual/en/function.date.php – Josh Beauregard Mar 27 '20 at 19:00
  • Oh, thanks! Now I can delete the whole variable definitions to count! How to convert that to a new date is still the problem I'm facing – Dani Dale Mar 27 '20 at 19:23
  • I feel like I'm holding a drill and trying to bore a hole by turning the entire drill instead of pushing the button to let the drill do the work – Dani Dale Mar 27 '20 at 19:28
  • No wait, it wouldn't be one line for each day, as the same date obviously occur on thirteen different date('z'), so that works, I suppose – Dani Dale Mar 27 '20 at 19:43
  • So this is where I'm at now. There is something wrong because the page displays April 1st, when it should be April 3rd... Dreamweaver is not giving any errors, but when it does, it mostly means a missing or incorrect character where it expected something else... https://pastebin.com/cKrnV1yF – Dani Dale Mar 27 '20 at 21:06
  • Does this answer your question? [Convert a date format in PHP](https://stackoverflow.com/questions/2487921/convert-a-date-format-in-php) – J. Robinson Mar 28 '20 at 04:23

2 Answers2

0

As noted think you want to look into date, strtotime and the php DateTime interfaces. It's not apparent to me what you're trying to do but you shouldn't have to build out large if/then statements to work with dates.

https://www.php.net/manual/en/function.date.php

https://www.php.net/manual/en/function.strtotime.php

https://www.php.net/manual/en/class.datetime

// Current Month
echo date('F') . '<br/>';

// Print January 1st of the current year
echo date('Y-01-01') . '<br/>';

// Current day of year
echo date('z') . '<br/>';

// strtotime is nice to parse any date format
$time = strtotime('now +60 days');
echo date('r', $time) . '<br/>';

// Get date from any day of year, note this starts from 0
$dayOfYear = 255;
$date = DateTime::createFromFormat( 'z' , $dayOfYear);
// Month
echo $date->format('F') . '<br/>';
// Day of the month
echo $date->format('d') . '<br/>';

March
2020-01-01
86
Tue, 26 May 2020 23:22:09 +0000
September
13
Dustin Butler
  • 818
  • 7
  • 22
  • I'm building a calendar site for the International Fixed Calendar, where the dates are different than the Gregorian calendar we've been using for the past 500 years. The IFC has 13 months with 28 days each, which is why I have to calculate the dates instead of just using the date() function. I'm heading towards getting todays IFC-date echoed on the page, but since IFC has 13 months with 28 days, todays IFC-date is April 4th, whereas the Gregorian date is March 28. https://pastebin.com/Jv2UNQ7i – Dani Dale Mar 28 '20 at 09:44
  • Latest pastebin with newest changes. This will be the last link since I made a user so I can edit pastes instead of creating new ones with every revision of the code. https://pastebin.com/tVmWMDC8 – Dani Dale Mar 28 '20 at 16:00
  • couldn't for the life of me figure out why the if, elseif value == x or x or x... and else-parameters didn't work, so it ended up with three lines of code for every day of the year... It ain't pretty, and the document consists of 1196 lines of code, but it works. – Dani Dale Mar 28 '20 at 16:57
0

Ok now I understand the problem. I think you can use the modulus operator and divide the Gregorian day of year by 28 to get the month and day for IFC. Then it's a matter of converting that into the correct month name.

ifcDate(0);

$time = strtotime('1/29/2020');
ifcDate(date('z', $time));

ifcDate(363);

// Today 
ifcDate(date('z'));

// Other dates
$time = strtotime('2/26/2020');
ifcDate(date('z', $time));

$time = strtotime('06/25/2020');
ifcDate(date('z', $time));

function ifcDate($day) {
  // gregorian day starts at 0
  $ifcMonth = ceil(($day + 1) / 28);
  $ifcDay = ($day % 28) + 1;

  // Month 7 is Sol
  if ($ifcMonth < 7) {
    $dateObj = DateTime::createFromFormat('!m', $ifcMonth);  
    $month = $dateObj->format('F'); 
  } elseif ($ifcMonth == 7) {
    $month = 'Sol';
  } else {
    $dateObj = DateTime::createFromFormat('!m', $ifcMonth - 1);  
    $month = $dateObj->format('F'); 
  }    

  echo "$ifcMonth $ifcDay" . "<br />";
  echo "$month $ifcDay" . "<br />";

}
Dustin Butler
  • 818
  • 7
  • 22