eclipse中使用anaconda引入numpy等包

工作一年,收获了很多,慢慢做些总结,提升工作效率,


工作中mysql使用的时间是一个UNIX时间戳:从1970年1月1日0点开始到当前时间的秒数,由于是int类型,很方便的适用于计算机处理,不仅仅是php和mysql的数据交互的一种格式,在各种客户端,也是数据交互的标准(android/IOS)等,因此如果只是保存和显示日期的时候,应该使用UNIX时间戳来计算日期和做为标准的日期格式。


工作中常用的流程是:将HTML页面的时间转化为时间戳保存到mysql中,从mysql中取出时间戳格式化展示在web或手机客户端。总之mysql中保存的时间是UNIX时间戳,然后被PHP格式化为合适的时间


介绍几个常用的函数


1.date(),2.mktime(),3.getdate(),4.strftime()


1.date()

PHP中获取时间和日期

使用date()函数:将时间戳或当前时间转化成格式化的字符串,例如:

echo date(‘Y-i-s‘);//输出2014-3-25


2.mktime()

使用mktime()将时间转化成UNIX时间戳

$timestamp = mktime();

获取当前时间戳有三种方法:

mktime(),time(),date(‘U‘)

mktime做时间运算

mktime(12,0,0,$mon,$day+10,$year);十天以后的时间戳


3.getdate()函数:

$today = getdate();

print_r($today);

//输出

Array
(
    [seconds] => 38
    [minutes] => 38
    [hours] => 22
    [mday] => 25
    [wday] => 2
    [mon] => 3
    [year] => 2014
    [yday] => 83
    [weekday] => Tuesday
    [month] => March
    [0] => 1395758318
)

使用checkdate()函数检验日期有效性


4.strftime()

格式化时间戳

mysql格式化时间

1.DATE_FORMAT()

2.UNIX_TIMESTAMP()返回格式化成UNIX时间戳的日期,例如:SELECT UNIX_TIMESTAMP(date) FROM table,这样就可以在PHP中处理了


PHP中格式化时间的函数比较少,介绍几个常用的格式化时间函数

	/**
	 * 
	 *将timestamp时间转化为x时x分x秒
	 * 
	 */
    public static function getTimeLong($seconds) {
        if (!$seconds) {
            return ‘0秒‘;
        }
        $ret = ‘‘;
        if ($seconds >= 3600) {
            $hours = (int)($seconds / 3600);
            $seconds = $seconds % 3600;
            if ($hours) {
                $ret .= ($hours . ‘时‘);
            }
        }
        if ($seconds >= 60) {
            $mi = (int)($seconds / 60);
            $seconds = $seconds % 60;
            if ($mi) {
                $ret .= ($mi . ‘分‘);
            }
        }
        if ($seconds) {
            $ret .= ($seconds . ‘秒‘);
        }
        return $ret;
    }


	/**
	 * 将相差timestamp转为如“1分钟前”,“3天前”等形式
	 *
	 * @param timestamp $ts_diff 当前时间 - 要格式化的timestamp
	 */
	public static function formatTime($ts_diff)
	{
		if ($ts_diff <=0)
		{
			return date(‘Y-m-d‘);
		}
		else if ( $ts_diff <= 3600 )
		{
			return max(1, (int)($ts_diff/60)) . ‘分钟前‘;
		}
		else if ( $ts_diff <= 86400 )
		{
			return ((int)($ts_diff/3600)) . ‘小时前‘;
		}
		else
		{
			return ((int)($ts_diff/86400)) . ‘天前‘;
		}
	}

    /** 将数字星期转换成字符串星期 weekNum2String($num)
     * @param int
     * @return string
     */
    public static function weekNum2String($num){
        switch($num){
            case 1:
                return ‘星期一‘;
            case 2:
                return ‘星期二‘;
            case 3:
                return ‘星期三‘;
            case 4:
                return ‘星期四‘;
            case 5:
                return ‘星期五‘;
            case 6:
                return ‘星期六‘;
            case 7:
                return ‘星期日‘;
            default:
                return ‘未知‘;
        }
    }



eclipse中使用anaconda引入numpy等包,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。