[原创] 自己写的日期转换为时间戳函数
想写一个时间戳转换成日期的函数,锻炼下自己的算法,呵呵。
函数代码:
运行测试:
函数代码:
1function timestamp( $year, $month = 1, $day = 1, $hour = 0, $minute = 0, $sec = 0 ) {
2 $tmp = ( $year - 1973 - ( $year -1973 ) % 4 ) / 4 * 126230400 + ($year - 1973) % 4 * 31536000 + 94608000;
3 $monthday = array( 0, 2678400, 5097600, 7776000, 10368000, 13046400, 15638400, 18316800, 20995200, 23587200, 26265600, 28857600 );
4 if ( $year % 4 == 0 && $month > 2 ) {
5 $tmp += $monthday[$month-1] + 86400 + $day * 86400 + $hour * 3600 + $minute * 60 + $sec;
6 return $tmp;
7 }
8 $tmp += $monthday[$month-1] + $day * 86400 + $hour * 3600 + $minute * 60 + $sec;
9 return $tmp;
10}
2 $tmp = ( $year - 1973 - ( $year -1973 ) % 4 ) / 4 * 126230400 + ($year - 1973) % 4 * 31536000 + 94608000;
3 $monthday = array( 0, 2678400, 5097600, 7776000, 10368000, 13046400, 15638400, 18316800, 20995200, 23587200, 26265600, 28857600 );
4 if ( $year % 4 == 0 && $month > 2 ) {
5 $tmp += $monthday[$month-1] + 86400 + $day * 86400 + $hour * 3600 + $minute * 60 + $sec;
6 return $tmp;
7 }
8 $tmp += $monthday[$month-1] + $day * 86400 + $hour * 3600 + $minute * 60 + $sec;
9 return $tmp;
10}
运行测试:
1$time = 1207101503;
2echo timestamp( 2008, 4, 2, 10 );
3echo "\n";
4echo gmdate( "Y-m-d H:i:s", timestamp( 2008, 4, 2, 10 ) );
2echo timestamp( 2008, 4, 2, 10 );
3echo "\n";
4echo gmdate( "Y-m-d H:i:s", timestamp( 2008, 4, 2, 10 ) );