PHP のストラトタイム

WBOY
リリース: 2024-08-29 12:54:57
オリジナル
1080 人が閲覧しました

PHP プログラミング言語には、日付と日付関連のタスクを処理するさまざまな関数があります。strtotime() はその 1 つです。 PHP 言語では日付をさまざまな用途に使用できます。関数 strtotime() は、PHP プログラミング言語の組み込み関数です。この関数は 2 つのパラメータを受け取り、そのうち 1 つは必須パラメータ、もう 1 つはオプションです。ビジネス要件に従って日付を操作するために、文字列を必須パラメーターとして渡すことができます。

広告 このカテゴリーの人気コース PHP 開発者 - 専門分野 | 8コースシリーズ | 3 つの模擬テスト

無料ソフトウェア開発コースを始めましょう

Web 開発、プログラミング言語、ソフトウェア テスト、その他

構文:

strtotime() 関数を使用するにはさまざまな方法があります。

strtotime(DateTime, Now);
ログイン後にコピー
  • 最初のパラメータ (DateTime) は、文字列形式の日付/時刻に関するものです。このパラメータは必須パラメータであり、この関数の操作中にスキップすることはできません。
  • 2 番目のパラメータ (Now) はオプションです。
  • PHP はバージョン 4 以降からこの機能をサポートしています。これまで、この機能にはさまざまな拡張が行われてきました。

PHP strtotime はどのように機能しますか?

この関数は、PHP 言語の基本関数の 1 つです。この機能を使用する前に追加のプラグインや拡張機能は必要ありません。この関数の構文に従うだけで、ビジネス要件に応じて使用を開始できます。

例: 誰かが現在の日付 (今日の日付を意味する) を知りたい場合は、この関数を使用できます。このためには、まず現在のタイムスタンプを取得する必要があり、その後、他の日付関数を使用して、そのタイムスタンプから正確な日付を取得できます。

コード:

<?php
$currentTimeStamp  =strtotime("now");
echo "The output at the time of writing this article was: " .$currentTimeStamp;
?>
ログイン後にコピー

このコード行により、現在のタイムスタンプが得られます。

出力:

PHP のストラトタイム

次に、別の関数を使用して、このタイムスタンプから人間が判読できる日付を取得します。

コード:

<?php
$current_date = date('m/d/Y', $currentTimeStamp);
echo "\n Today is: " . $current_date;
?>
ログイン後にコピー

出力:

PHP のストラトタイム

PHP strtotime の例

言及されている例を以下に示します:

例 #1

ここでは、上記のコードをプログラムとして結合することから始めて、出力を確認します。

コード

<?php
$currentTimeStamp  =strtotime("now"); // this line will gives us the current timestamp
echo "The current timestamp is: ". $currentTimeStamp;
$current_date = date('m/d/Y', $currentTimeStamp); // converting the timestamp into the readable format
echo "\n Today is: " . $current_date;
?>
ログイン後にコピー

出力:

PHP のストラトタイム

この出力は、このプログラムを作成して実行した時点のものであることに注意してください。現在の日付に従って異なる出力が表示されます。

例 #2

日付をタイムスタンプに変更します。ご存知のとおり、文字列の日付をタイムスタンプに変更するのは、strtotime() 関数の主な機能です。

コード:

<?php
$date = '27/02/2010';
echo "Actual Date: ". $date."\n";
$date = str_replace('/', '-', $date);
// The nature of the strtotime() function is it takes the string date as a parameter in a specified format only.
// So we are converting the / with the - before passing it to this function we are talking about.
echo "After changing to format of the specified Date: ". $date."\n";
// Now changing the date again to the human-readable form with the different format...
echo "This is Date: ". date('Y-m-d', strtotime($date));
?>
ログイン後にコピー

出力:

PHP のストラトタイム

例 #3

文字列の日付を実際の日付に変更します。

コード:

<?php
echo strtotime("today") . "\n"; // this will give us the timestamp of today
echo strtotime("27 Mar 2020") . "\n";  // this will give us the timestamp of 27 Mar 2020
echo strtotime("+2 hours") . "\n"; // Timestamp of 2 hours later from now
echo strtotime("2 hours") . "\n"; // This will also give the Timestamp of 2 hours later from now
echo strtotime("-2 hours") . "\n"; // This will  give the Timestamp of 2 hours before from now
echo strtotime("3 week") . "\n";   // Timestamp of 3 weeks later from now
echo strtotime("last Monday") . "\n" ;      // This will give the Timestamp of the last Monday
echo strtotime("next Monday");      // This will give the Timestamp of the coming Monday
?>
ログイン後にコピー

上記のコードを実行すると、さまざまな出力が表示されます。それは、このプログラムをいつ実行するかによって完全に異なります。

出力:

PHP のストラトタイム

例 #4

上記のプログラムをもう少し変更して、これらのタイムスタンプとともに人間が判読できる日付にします。また、インドの観点に従って日付を取得できるように、現在のタイムゾーンをアジア/コルカタに設定しています。要件に応じてタイムゾーンを任意のゾーンに設定できます。

コード:

<?php
date_default_timezone_set('Asia/Kolkata');
echo strtotime("today") ;
echo " Today is -  " .date('Y-m-d H:i:s', strtotime("today"))."\n";
echo strtotime("27 Mar 2020") ;
echo " Today is -  " .date('Y-m-d H:i:s', strtotime("27 Mar 2020"))."\n";
echo strtotime("+2 hours") ;
echo " After 2 hours from now -  " .date('Y-m-d H:i:s', strtotime("+2 hours"))."\n";
echo strtotime("-2 hours") ;
echo " Before 2 hours from now -   " .date('Y-m-d H:i:s', strtotime("-2 hours"))."\n";
echo strtotime("last Monday") ;
echo " Date and Time of Last Monday " .date('Y-m-d H:i:s', strtotime("last Monday"))."\n";
echo strtotime("next Monday");      // This will give the Timestamp of the coming Monday
echo " Date and Time of coming Monday  " .  date('Y-m-d H:i:s', strtotime("next Monday"))."\n";
?>
ログイン後にコピー

出力:

PHP のストラトタイム

結論

必要に応じていつでもこの strtotime() PHP 組み込み関数を使用できます。開発者またはコーダーは、パラメータとして指定された文字列の日付を認識する必要があります。文字列の日付を Unix タイムスタンプに変更する必要があるときはいつでも、この関数を使用できます。

以上がPHP のストラトタイムの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
php
ソース:php
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!