webForumDet fria alternativet

negativ unixtime

1 svar · 231 visningar · startad av startail

startailMedlem sedan sep. 2000155 inlägg
#1

Hur får man fram ett negativt unixtime format innan 1970 ?

Tex:

$d = "1936-07-19";
print "strtotime: " . strtotime($d) ."<br />";
print "strtotime and date: " . date("Y-m-d", strtotime($d)) ."<br /><br />";

$d = "-1055714400";
print "strtotime: " . $d ."<br />";
print "strtotime and date: " . date("Y-m-d", $d) ."<br /><br />";

Det första ger -1 och det andra ger 1936-07-19.

Hur får man om 1936-07-19 till ett negativt unixtime format ?

Vore väldigt tacksam för ett svar.

startailMedlem sedan sep. 2000155 inlägg
#2

Lösning på problemet med negativa unixtider kan lösas med...

function maketime ($hour = false, $minute = false, $second = false, $month = false, $date = false, $year = false)
   {
       // For centuries, the Egyptians used a (12 * 30 + 5)-day calendar
       // The Greek began using leap-years in around 400 BC
       // Ceasar adjusted the Roman calendar to start with Januari rather than March
       // All knowledge was passed on by the Arabians, who showed an error in leaping
       // In 1232 Sacrobosco (Eng.) calculated the error at 1 day per 288 years
       //    In 1582, Pope Gregory XIII removed 10 days (Oct 15-24) to partially undo the
       // error, and he instituted the 400-year-exception in the 100-year-exception,
       // (notice 400 rather than 288 years) to undo the rest of the error
       // From about 2044, spring will again coincide with the tropic of Cancer
       // Around 4100, the calendar will need some adjusting again
  
       if ($hour === false)  $hour  = Date ("G");
       if ($minute === false) $minute = Date ("i");
       if ($second === false) $second = Date ("s");
       if ($month === false)  $month  = Date ("n");
       if ($date === false)  $date  = Date ("j");
       if ($year === false)  $year  = Date ("Y");
  
       if ($year >= 1970) return mktime ($hour, $minute, $second, $month, $date, $year);
  
       //    date before 1-1-1970 (Win32 Fix)
       $m_days = Array (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
       if ($year % 4 == 0 && ($year % 100 > 0 || $year % 400 == 0))
       {
           $m_days[1] = 29; // non leap-years can be: 1700, 1800, 1900, 2100, etc.
	       $d_year = 1970 - $year;
    	   $days = 0 - $d_year * 365 - 1;
       }
       elseif ($year % 4 == 3 && ($year % 100 > 0 || $year % 400 == 0))
       {
	       $d_year = 1970 - $year;
	       $days = 0 - $d_year * 365 - 1;
       }
  	   else {
	       //    go backward (-), based on $year
    	   $d_year = 1970 - $year;
    	   $days = 0 - $d_year * 365;
	   }
       $days -= floor ($d_year / 4);          // compensate for leap-years
       $days += floor (($d_year - 70) / 100);  // compensate for non-leap-years
       $days -= floor (($d_year - 370) / 400); // compensate again for giant leap-years
          
       //    go forward (+), based on $month and $date
       for ($i = 1; $i < $month; $i++)
       {
           $days += $m_days [$i - 1];
       }
       $days += $date - 1;
  
       //    go forward (+) based on $hour, $minute and $second
       $stamp = $days * 86400;
       $stamp += $hour * 3600;
       $stamp += $minute * 60;
       $stamp += $second;
  
       return $stamp;
}
125 ms totalt · 3 externa anrop · v20260731065814-full.86db40fa
0 ms — hämta forumlista (cache)
0 ms — hämta statistik (cache)
122 ms — hämta tråd, inlägg och bilagor (db)