PHP中几个常用的日期及时间格式转换及比较函数

2017-04-22 23:20 阅读 1,351 次 评论 0 条

显示日期:echo date( "Y-m-d", time() );
显示时间:echo date( "H:i:s", time() );
格式化用法,如:format_date(date( "Y-m-d", time() ))
function format_date( $STRING1 )
{
$STRING1 = str_replace( "-0", "-", $STRING1 );
$STR = strtok( $STRING1, "-" );
$STRING2 = $STR."年";
$STR = strtok( "-" );
$STRING2 .= $STR."月";
$STR = strtok( " " );
$STRING2 .= $STR."日";
return $STRING2;
}

function format_date_short1( $STRING1 )
{
$STRING1 = str_replace( "-0", "-", $STRING1 );
$STR = strtok( $STRING1, "-" );
$STRING2 = $STR."年";
$STR = strtok( "-" );
$STRING2 .= $STR."月";
return $STRING2;
}

function format_date_short2( $STRING1 )
{
$STRING1 = str_replace( "-0", "-", $STRING1 );
$STR = strtok( $STRING1, "-" );
$STR = strtok( "-" );
$STRING2 .= $STR."月";
$STR = strtok( " " );
$STRING2 .= $STR."日";
return $STRING2;
}

function get_week( $STRING1 )
{
$STR = strtok( $STRING1, "-" );
$YEAR = $STR;
$STR = strtok( "-" );
$MONTH = $STR;
$STR = strtok( " " );
$DAY = $STR;
$TIME1 = mktime( 0, 0, 0, $MONTH, $DAY, $YEAR );
switch ( date( "w", $TIME1 ) )
{
case 0 :
return "日";
case 1 :
return "一";
case 2 :
return "二";
case 3 :
return "三";
case 4 :
return "四";
case 5 :
return "五";
case 6 :
return "六";
}
}

function format_money( $STR )
{
if ( $STR == "" )
{
return "";
}
if ( $STR == ".00" )
{
return "0.00";
}
$TOK = strtok( $STR, "." );
if ( strcmp( $STR, $TOK ) == "0" )
{
$STR .= ".00";
}
else
{
$TOK = strtok( "." );
$I = 1;
for ( ; $I <= 2 - strlen( $TOK ); ++$I )
{
$STR .= "0";
}
}
if ( substr( $STR, 0, 1 ) == "." )
{
$STR = "0".$STR;
}
return $STR;
}

function compare_date( $DATE1, $DATE2 )
{
$STR = strtok( $DATE1, "-" );
$YEAR1 = $STR;
$STR = strtok( "-" );
$MON1 = $STR;
$STR = strtok( "-" );
$DAY1 = $STR;
$STR = strtok( $DATE2, "-" );
$YEAR2 = $STR;
$STR = strtok( "-" );
$MON2 = $STR;
$STR = strtok( "-" );
$DAY2 = $STR;
if ( $YEAR2 < $YEAR1 )
{
return 1;
}
if ( $YEAR1 < $YEAR2 )
{
return -1;
}
if ( $MON2 < $MON1 )
{
return 1;
}
if ( $MON1 < $MON2 )
{
return -1;
}
if ( $DAY2 < $DAY1 )
{
return 1;
}
if ( $DAY1 < $DAY2 )
{
return -1;
}
return 0;
}

function compare_time( $TIME1, $TIME2 )
{
$STR = strtok( $TIME1, ":" );
$HOUR1 = $STR;
$STR = strtok( ":" );
$MIN1 = $STR;
$STR = strtok( ":" );
$SEC1 = $STR;
$STR = strtok( $TIME2, ":" );
$HOUR2 = $STR;
$STR = strtok( ":" );
$MIN2 = $STR;
$STR = strtok( ":" );
$SEC2 = $STR;
if ( $HOUR2 < $HOUR1 )
{
return 1;
}
if ( $HOUR1 < $HOUR2 )
{
return -1;
}
if ( $MIN2 < $MIN1 )
{
return 1;
}
if ( $MIN1 < $MIN2 )
{
return -1;
}
if ( $SEC2 < $SEC1 )
{
return 1;
}
if ( $SEC1 < $SEC2 )
{
return -1;
}
return 0;
}

function compare_date_time( $DATE_TIME1, $DATE_TIME2 )
{
if ( $DATE_TIME1 == NULL || strlen( $DATE_TIME1 ) == 0 || $DATE_TIME2 == NULL || strlen( $DATE_TIME2 ) == 0 )
{
return -1;
}
$DATE_TIME1_ARRY = explode( " ", $DATE_TIME1 );
$DATE_TIME2_ARRY = explode( " ", $DATE_TIME2 );
if ( compare_date( $DATE_TIME1_ARRY[0], $DATE_TIME2_ARRY[0] ) == 1 )
{
return 1;
}
if ( compare_date( $DATE_TIME1_ARRY[0], $DATE_TIME2_ARRY[0] ) == 0 )
{
if ( compare_time( $DATE_TIME1_ARRY[1], $DATE_TIME2_ARRY[1] ) == 1 )
{
return 1;
}
if ( compare_time( $DATE_TIME1_ARRY[1], $DATE_TIME2_ARRY[1] ) == 0 )
{
return 0;
}
return -1;
}
return -1;
}

版权声明:本文著作权归原作者所有,欢迎分享本文,谢谢支持!
转载请注明:PHP中几个常用的日期及时间格式转换及比较函数 | 猿笔记

发表评论


表情