検索

php の時刻と日付ツール クラス

<?php
DateTimeUtils::addDate('2012-12-01',1,'y');
DateTimeUtils::getWeekDay('2012/10/01','/');
DateTimeUtils::isLeapYear('2012');
class DateTimeUtils {
	/**
	 * Checks for leap year, returns true if it is. No 2-digit year check. Also
	 * handles julian calendar correctly.
	 * @param integer $year year to check
	 * @return boolean true if is leap year
	 */
	public static function isLeapYear($year)
	{
		$year = self::digitCheck($year);
		if ($year % 4 != 0)
			return false;

		if ($year % 400 == 0)
			return true;
		// if gregorian calendar (>1582), century not-divisible by 400 is not leap
		else if ($year > 1582 && $year % 100 == 0 )
			return false;
		return true;
	}

	/**
	 * Fix 2-digit years. Works for any century.
	 * Assumes that if 2-digit is more than 30 years in future, then previous century.
	 * @param integer $y year
	 * @return integer change two digit year into multiple digits
	 */
	protected static function digitCheck($y)
	{
		if ($y < 100){
			$yr = (integer) date("Y");
			$century = (integer) ($yr /100);

			if ($yr%100 > 50) {
				$c1 = $century + 1;
				$c0 = $century;
			} else {
				$c1 = $century;
				$c0 = $century - 1;
			}
			$c1 *= 100;
			// if 2-digit year is less than 30 years in future, set it to this century
			// otherwise if more than 30 years in future, then we set 2-digit year to the prev century.
			if (($y + $c1) < $yr+30) $y = $y + $c1;
			else $y = $y + $c0*100;
		}
		return $y;
	}

	/**
	 * Returns 4-digit representation of the year.
	 * @param integer $y year
	 * @return integer 4-digit representation of the year
	 */
	public static function get4DigitYear($y)
	{
		return self::digitCheck($y);
	}
	/**
	 * Checks to see if the year, month, day are valid combination.
	 * @param integer $y year
	 * @param integer $m month
	 * @param integer $d day
	 * @return boolean true if valid date, semantic check only.
	 */
	public static function isValidDate($y,$m,$d)
	{
		return checkdate($m, $d, $y);
	}
	public static function checkDate($date, $separator = "-") { //检查日期是否合法日期
		$dateArr = explode ($separator, $date);
		if (is_numeric ( $dateArr [0] ) && is_numeric ( $dateArr [1] ) && is_numeric ( $dateArr [2] )) {
			return checkdate ( $dateArr [1], $dateArr [2], $dateArr [0] );
		}
		return false;
	}
	/**
	 * Checks to see if the hour, minute and second are valid.
	 * @param integer $h hour
	 * @param integer $m minute
	 * @param integer $s second
	 * @param boolean $hs24 whether the hours should be 0 through 23 (default) or 1 through 12.
	 * @return boolean true if valid date, semantic check only.
	 * @since 1.0.5
	 */
	public static function isValidTime($h,$m,$s,$hs24=true)
	{
		if($hs24 && ($h < 0 || $h > 23) || !$hs24 && ($h < 1 || $h > 12)) return false;
		if($m > 59 || $m < 0) return false;
		if($s > 59 || $s < 0) return false;
		return true;
	}
	public static function checkTime($time, $separator = ":") { //检查时间是否合法时间  
		$timeArr = explode ($separator, $time );
		if (is_numeric ( $timeArr [0] ) && is_numeric ( $timeArr [1] ) && is_numeric ( $timeArr [2] )) {
			if (($timeArr [0] >= 0 && $timeArr [0] <= 23) && ($timeArr [1] >= 0 && $timeArr [1] <= 59) && ($timeArr [2] >= 0 && $timeArr [2] <= 59))
				return true;
			else
				return false;
		}
		return false;
	}
	
	public static function addDate($date, $int, $unit = "d") { //日期的增加
		$dateArr = explode ( "-", $date );
		$value [$unit] = $int;
		return date ( "Y-m-d", mktime ( 0, 0, 0, $dateArr [1] + $value ['m'], $dateArr [2] + $value ['d'], $dateArr [0] + $value ['y'] ) );
	
	}
	
	public static function addDayTimestamp($ntime, $aday) { //取当前时间后几天,天数增加单位为1
		$dayst = 3600 * 24;
		$oktime = $ntime + ($aday * $dayst);
		return $oktime;
	}
	
	public static function dateDiff($date1, $date2, $unit = "d") { //时间比较函数,返回两个日期相差几秒、几分钟、几小时或几天  
		switch ($unit) {
			case 's' :
				$dividend = 1;
				break;
			case 'i' :
				$dividend = 60;
				break;
			case 'h' :
				$dividend = 3600;
				break;
			case 'd' :
				$dividend = 86400;
				break;
			default :
				$dividend = 86400;
		}
		$time1 = strtotime ( $date1 );
		$time2 = strtotime ( $date2 );
		if ($time1 && $time2)
			return ( float ) ($time1 - $time2) / $dividend;
		return false;
	}
	//2011-12-06  =>
	public static function getWeekDay($date, $separator = "-") { //计算出给出的日期是星期几
		$dateArr = explode ($separator, $date);
		return date ( "w", mktime ( 0, 0, 0, $dateArr [1], $dateArr [2], $dateArr [0] ) );
	}
	
	public static function floorTime($seconds) { //让日期显示为:XX天XX年以前 
		$times = '';
		$days = floor ( ($seconds / 86400) % 30 );
		$hours = floor ( ($seconds / 3600) % 24 );
		$minutes = floor ( ($seconds / 60) % 60 );
		$seconds = floor ( $seconds % 60 );
		if ($seconds >= 1)
			$times .= $seconds . '秒';
		if ($minutes >= 1)
			$times = $minutes . '分钟 ' . $times;
		if ($hours >= 1)
			$times = $hours . '小时 ' . $times;
		if ($days >= 1)
			$times = $days . '天';
		if ($days > 30)
			return false;
		$times .= '前';
		return str_replace ( " ", '', $times );
	}
	
	public static function transDateToChs($date) {
		if (empty ( $date ))
			return '今日';
		$y = date ( 'Y', strtotime ( $date ) );
		$m = date ( 'm', strtotime ( $date ) );
		$d = date ( 'd', strtotime ( $date ) );
		return $y . '年' . $m . '月' . $d . '日';
	}
	
	// 08/31/2004 => 2004-08-31
	public static function TransDateUI($datestr, $type = 'Y-m-d') {
		if ($datestr == Null)
			return Null;
		$target = $datestr;
		$arr_date = preg_split ( "/\//", $target );
		$monthstr = $arr_date [0];
		$daystr = $arr_date [1];
		$yearstr = $arr_date [2];
		$result = date ( $type, mktime ( 0, 0, 0, $monthstr, $daystr, $yearstr ) );
		return $result;
	}
	
	// 12/20/2004 10:55 AM => 2004-12-20 10:55:00
	public static function TransDateTimeUI($datestr, $type = 'Y-m-d H:i:s') {
		if ($datestr == Null)
			return Null;
		$target = $datestr;
		$arr_date = preg_split ( "/\/|\s|:/", $target );
		$monthstr = $arr_date [0];
		$daystr = $arr_date [1];
		$yearstr = $arr_date [2];
		$hourstr = $arr_date [3];
		$minutesstr = $arr_date [4];
		$result = date ( $type, mktime ( $hourstr, $minutesstr, 0, $monthstr, $daystr, $yearstr ) );
		return $result;
	}
	
	// 2004-08-31 => 08/31/2004
	public static function TransDateDB($datestr, $type = 'm/d/Y') {
		if ($datestr == Null)
			return Null;
		if ($datestr == '0000-00-00')
			return Null;
		$target = $datestr;
		$arr_date = preg_split ( "/-/", $target );
		$monthstr = $arr_date [1];
		$daystr = $arr_date [2];
		$yearstr = $arr_date [0];
		$result = date ( $type, mktime ( 0, 0, 0, $monthstr, $daystr, $yearstr ) );
		return $result;
	}
	
	// 2004-08-31 10:55:00 => 12/20/2004 10:55 AM 
	public static function TransDateTimeDB($datestr, $type = 'm/d/Y h:i A') {
		if ($datestr == Null)
			return Null;
		$target = $datestr;
		$arr_date = preg_split ( "/-|\s|:/", $target );
		$monthstr = $arr_date [1];
		$daystr = $arr_date [2];
		$yearstr = $arr_date [0];
		$hourstr = $arr_date [3];
		$minutesstr = $arr_date [4];
		$secondstr = $arr_date [5];
		$result = date ( $type, mktime ( $hourstr, $minutesstr, $secondstr, $monthstr, $daystr, $yearstr ) );
		return $result;
	}
}
?>
?

声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
PHP対Python:違いを理解しますPHP対Python:違いを理解しますApr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHPは、シンプルな構文と高い実行効率を備えたWeb開発に適しています。 2。Pythonは、簡潔な構文とリッチライブラリを備えたデータサイエンスと機械学習に適しています。

PHP:それは死にかけていますか、それとも単に適応していますか?PHP:それは死にかけていますか、それとも単に適応していますか?Apr 11, 2025 am 12:13 AM

PHPは死にかけていませんが、常に適応して進化しています。 1)PHPは、1994年以来、新しいテクノロジーの傾向に適応するために複数のバージョンの反復を受けています。 2)現在、電子商取引、コンテンツ管理システム、その他の分野で広く使用されています。 3)PHP8は、パフォーマンスと近代化を改善するために、JITコンパイラおよびその他の機能を導入します。 4)Opcacheを使用してPSR-12標準に従って、パフォーマンスとコードの品質を最適化します。

PHPの未来:適応と革新PHPの未来:適応と革新Apr 11, 2025 am 12:01 AM

PHPの将来は、新しいテクノロジーの傾向に適応し、革新的な機能を導入することで達成されます。1)クラウドコンピューティング、コンテナ化、マイクロサービスアーキテクチャに適応し、DockerとKubernetesをサポートします。 2)パフォーマンスとデータ処理の効率を改善するために、JITコンパイラと列挙タイプを導入します。 3)パフォーマンスを継続的に最適化し、ベストプラクティスを促進します。

PHPの抽象クラスまたはインターフェイスに対して、いつ特性を使用しますか?PHPの抽象クラスまたはインターフェイスに対して、いつ特性を使用しますか?Apr 10, 2025 am 09:39 AM

PHPでは、特性は方法が必要な状況に適していますが、継承には適していません。 1)特性により、クラスの多重化方法が複数の継承の複雑さを回避できます。 2)特性を使用する場合、メソッドの競合に注意を払う必要があります。メソッドの競合は、代替およびキーワードとして解決できます。 3)パフォーマンスを最適化し、コードメンテナビリティを改善するために、特性の過剰使用を避け、その単一の責任を維持する必要があります。

依存関係噴射コンテナ(DIC)とは何ですか?また、なぜPHPで使用するのですか?依存関係噴射コンテナ(DIC)とは何ですか?また、なぜPHPで使用するのですか?Apr 10, 2025 am 09:38 AM

依存関係噴射コンテナ(DIC)は、PHPプロジェクトで使用するオブジェクト依存関係を管理および提供するツールです。 DICの主な利点には、次のものが含まれます。1。デカップリング、コンポーネントの独立したもの、およびコードの保守とテストが簡単です。 2。柔軟性、依存関係を交換または変更しやすい。 3.テスト可能性、単体テストのために模擬オブジェクトを注入するのに便利です。

通常のPHPアレイと比較して、SPL SPLFIXEDARRAYとそのパフォーマンス特性を説明してください。通常のPHPアレイと比較して、SPL SPLFIXEDARRAYとそのパフォーマンス特性を説明してください。Apr 10, 2025 am 09:37 AM

SplfixedArrayは、PHPの固定サイズの配列であり、高性能と低いメモリの使用が必要なシナリオに適しています。 1)動的調整によって引き起こされるオーバーヘッドを回避するために、作成時にサイズを指定する必要があります。 2)C言語アレイに基づいて、メモリと高速アクセス速度を直接動作させます。 3)大規模なデータ処理とメモリに敏感な環境に適していますが、サイズが固定されているため、注意して使用する必要があります。

PHPは、ファイルを安全に処理する方法をどのように処理しますか?PHPは、ファイルを安全に処理する方法をどのように処理しますか?Apr 10, 2025 am 09:37 AM

PHPは、$ \ _ファイル変数を介してファイルのアップロードを処理します。セキュリティを確保するための方法には次のものが含まれます。1。アップロードエラー、2。ファイルの種類とサイズを確認する、3。ファイル上書きを防ぐ、4。ファイルを永続的なストレージの場所に移動します。

Null Coulescingオペレーター(??)およびNull Coulescing Assignment Operator(?? =)とは何ですか?Null Coulescingオペレーター(??)およびNull Coulescing Assignment Operator(?? =)とは何ですか?Apr 10, 2025 am 09:33 AM

JavaScriptでは、nullcoalescingoperator(??)およびnullcoalescingsignmentoperator(?? =)を使用できます。 1.??最初の非潜水金または非未定されたオペランドを返します。 2.??これらの演算子は、コードロジックを簡素化し、読みやすさとパフォーマンスを向上させます。

See all articles

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

AI Hentai Generator

AI Hentai Generator

AIヘンタイを無料で生成します。

ホットツール

WebStorm Mac版

WebStorm Mac版

便利なJavaScript開発ツール

DVWA

DVWA

Damn Vulnerable Web App (DVWA) は、非常に脆弱な PHP/MySQL Web アプリケーションです。その主な目的は、セキュリティ専門家が法的環境でスキルとツールをテストするのに役立ち、Web 開発者が Web アプリケーションを保護するプロセスをより深く理解できるようにし、教師/生徒が教室環境で Web アプリケーションを教え/学習できるようにすることです。安全。 DVWA の目標は、シンプルでわかりやすいインターフェイスを通じて、さまざまな難易度で最も一般的な Web 脆弱性のいくつかを実践することです。このソフトウェアは、

SublimeText3 Linux 新バージョン

SublimeText3 Linux 新バージョン

SublimeText3 Linux 最新バージョン

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser は、オンライン試験を安全に受験するための安全なブラウザ環境です。このソフトウェアは、あらゆるコンピュータを安全なワークステーションに変えます。あらゆるユーティリティへのアクセスを制御し、学生が無許可のリソースを使用するのを防ぎます。

MantisBT

MantisBT

Mantis は、製品の欠陥追跡を支援するために設計された、導入が簡単な Web ベースの欠陥追跡ツールです。 PHP、MySQL、Web サーバーが必要です。デモおよびホスティング サービスをチェックしてください。