メモ  PHPによる日付・時刻の取得

PHP (PHP: Hypertext Preprocessor) は、HTMLファイル内に記述するタイプのスクリプト言語です。  通常のCGIとして使用できますが、PHPモジュールをApacheサーバーに組み込むことにより、Perl/CGIと比較して処理速度の高速化、サーバー負荷の低減が可能です。
日本PHPユーザ会,  Japan PHP User's GroupJapan PHP User's Group
What is PHP?
PHP is a widely-used general-purpose scripting language that is especially suited for Web development and can be embedded into HTML.
The PHP Group,  PHP: Hypertext PreprocessorPHP: Hypertext Preprocessor

このサイトでは、全てのページにPHPを使っています。
なぜPHPを知ったのか、今はもう存在しませんが前の無料プロバイダーのサイトで紹介していたページをこちらへ移しました。
PHPっていいな

<?php
print gmdate("Y/m/d H:i", time()+9*60*60);
?>
実行結果: 2008/07/06 11:13

日本時間を取得する方法としては他に putenv() 関数を使って環境変数の"TZ"(Time Zone)を変更してやる方法があります。  そうすることで、time() がそのまま日本時間を返すようになります。  但し、PHPがSafe Modeで動作している場合はユーザが設定できる環境変数の種類に制限があるので、無料のレンタルサーバなどではこの方法が使えない場合が多いです。  Safe Modeの時にユーザが自由に設定できる環境変数の種類は php.ini の設定 (safe_mode_allowed_env_vars)に依ります。  デフォルトでは "PHP_" で始まる環境変数のみ設定できます。(つまり"TZ"は設定できません)
引用: http://www.spencernetwork.org/memo/tips-1.php PHP SAMPLES & TIPS

putenv("TZ=JST-9")
putenv("TZ=Asia/Tokyo")

<?php

echo date("Y/m/d H:i", time());
?>
実行結果: 2008/07/06 02:13 UTC
<?php
putenv("TZ=JST-9");
echo date("Y/m/d H:i", time());
?>
実行結果: 2008/07/06 11:13 JST

<?php
$lastmod = date ("Y/m/d H:i:s", getlastmod());
echo "Last Modified: " . date ($lastmod);
?>
実行結果: Last Modified: 2008/01/01 00:33:13

<?php
$lastmod = gmdate ("Y/m/d H:i:s", getlastmod()+9*60*60);
echo "Last Modified: " . date ($lastmod);
?>
実行結果: Last Modified: 2008/01/01 00:33:13

<?php
$filename = './../ee-eh/electriccharges.php';
if (file_exists($filename)) {
echo "$filename was last modified: " . gmdate ("Y/m/d H:i:s", filemtime($filename)+9*60*60);
}
?>
実行結果: ./../ee-eh/electriccharges.php was last modified: 2008/06/28 12:29:23

<?php
$elapsed = gmdate("z", time()+9*60*60);
print "住まいして" . ($elapsed+17+365+366+365+365+365) . "日";
?>
実行結果: 住まいして2030日

<?=
intval((strtotime("today") - strtotime("15 December 2002")) / 86400) . " days"
?>
実行結果: 2030 days

Message # 1009287:
Date: 01/24/02 01:58
By: stewartg
Subject: simple date calculation (days difference)
to simply get the number of days that have passed (or in the future) from another date you can do:
<?= intval((strtotime($end) - strtotime($start)) / 86400) . " days" ?>
this will accept the dates in any format (ie. mm/dd/yy, yyyy-mm-dd, month day year, today, now, etc)
see http://www.php.net/manual/en/function.strtotime.php for more details on strtotime().
Reference: http://www.phpbuilder.com/annotate/message.php3?id=1009287 PHPBuilder.com. the best resource for PHP tutorials, templates, PHP manuals, content management systems, scripts, classes and more.

<?= expression ?> This is a shortcut for "<? echo expression ?>"
Reference: http://www.php.net/manual/en/language.basic-syntax.php

<?php
function elapsed($year, $month, $day, $hour, $minute) {

// No copyright, use and fiddle with as you see fit, just don't steal credit for it,
// as it is based on the work of myself and several other people.
// Call the function with the command elapsed(yyyy,mm,dd,hh,mm), for example:
// elapsed(1974,5,14,06,10) will produce the elapsed time since my birth date.

// This script will automatically omit parts that return a zero value, so that 1 year,
// 3 months, 0 days, 0 hours and 0 minutes will not show the days, hours and minutes,
// as that would be quite pointless and ugly.

$todayMonth = date(n);
$todayDay = date(j);
$todayHour = date(G);
$todayMinute = date(i);
$monthday = date(nj);
$link = $month.$day;
$datepoint = $link;

if ($monthday < $datepoint) $years = date(Y) - $year - 1;
else $years = date(Y) - $year;
if ($years < 1) $yearWord = '';
elseif ($years == 1) $yearWord = 'year,';
elseif ($years > 1) $yearWord = 'years,';
if ($todayMonth > $month) {
if ($todayDay < $day) $months = $todayMonth - $month -1;
else $months = $todayMonth - $month;
} else {
$between = $month - $todayMonth;
$plus = 12 - $month;
if ($todayDay < $day) $months = $month + $plus - $between -1;
else $months = $month + $plus - $between;
}
if ($months == 1) $monthWord = 'month,';
elseif ($months > 1) $monthWord = 'months,';
if ($todayDay > $day) {
if ($todayHour < $hour) $days = $todayDay - $day -1;
else $days = $todayDay - $day;
} else {
$between = $day - $todayDay;
$daysToEndOfMonth = date(t) - $day;
if (date(t) == 28) $plus = $daysToEndOfMonth;
if (date(t) == 29) $plus = $daysToEndOfMonth;
if (date(t) == 30) $plus = $daysToEndOfMonth;
if (date(t) == 31) $plus = $daysToEndOfMonth;
if ($todayHour < $hour) $days = $day + $plus - $between - 1;
else $days = $day + $plus - $between;
}
if ($days == 1) $dayWord = 'day,';
else $dayWord = 'days,';
if ($todayHour > $hour) {
if ($todayMinute < $minute) $hours = $todayHour - $hour - 1;
else $hours = $todayHour - $hour;
} else {
$between = $hour - $todayHour;
$plus = 24 - $hour;
if ($todayMinute < $minute) $hours = $hour + $plus - $between - 1;
else $hours = $hour + $plus - $between;
}
if ($hours == 1) $hourWord = 'hour,';
if ($hours > 1) $hourWord = 'hours,';
if ($todayMinute > $minute) {
$minutes = $todayMinute - $minute;
} else {
$between = $minute - $todayMinute;
$plus = 60 - $minute;
$minutes = $minute + $plus - $between;
}
if ($minutes == 60) $minutes = 0;
if ($minutes == 1) $minuteWord = 'minute';
elseif ($minutes > 1) $minuteWord = 'minutes';
if ($years == 0) { $years = ''; $yearWord = ''; }
if ($months == 0) { $months = ''; $monthWord = ''; }
if ($days == 0) { $days = ''; $dayWord = ''; }
if ($hours == 0) { $hours = ''; $hourWord = ''; }
if ($minutes == 0) { $minutes = ''; $minuteWord = ''; }
echo $years.' '.$yearWord.' '.$months.' '.$monthWord.' '.$days.' '.$dayWord.' '.$hours.' '.$hourWord.' '.$minutes.' '.$minuteWord.'';
}
elapsed(2002,12,15,17,00)
?>
実行結果: 5 years, 6 months, 21 days, 18 hours, 13 minutes
Reference: http://www.kathmann.com/up/ > http://www.kathmann.com/up/php/elapsed.txt

<?
// This is a simple script to calculate the difference between two dates
// and express it in years, months and days
//
// use as in: "my daughter is 4 years, 2 month and 17 days old" ... :-)
//
// Feel free to use this script for whatever you want
//
// version 0.1 / 2002-10-3
//
// please send comments and feedback to webmaster@lotekk.net
//

// ****************************************************************************

// configure the base date here
$base_day = 15; // no leading "0"
$base_mon = 12; // no leading "0"
$base_yr = 2002; // use 4 digit years!

// get the current date (today) -- change this if you need a fixed date
$current_day = date ("j");
$current_mon = date ("n");
$current_yr = date ("Y");

// and now .... calculate the difference! :-)

// overflow is always caused by max days of $base_mon
// so we need to know how many days $base_mon had
$base_mon_max = date ("t",mktime (0,0,0,$base_mon,$base_day,$base_yr));

// days left till the end of that month
$base_day_diff = $base_mon_max - $base_day;

// month left till end of that year
// substract one to handle overflow correctly
$base_mon_diff = 12 - $base_mon - 1;

// start on jan 1st of the next year
$start_day = 1;
$start_mon = 1;
$start_yr = $base_yr + 1;

// difference to that 1st of jan
$day_diff = ($current_day - $start_day) + 1; // add today
$mon_diff = ($current_mon - $start_mon) + 1; // add current month
$yr_diff = ($current_yr - $start_yr);

// and add the rest of $base_yr
$day_diff = $day_diff + $base_day_diff;
$mon_diff = $mon_diff + $base_mon_diff;

// handle overflow of days
if ($day_diff >= $base_mon_max)
{
$day_diff = $day_diff - $base_mon_max;
$mon_diff = $mon_diff + 1;
}

// handle overflow of years
if ($mon_diff >= 12)
{
$mon_diff = $mon_diff - 12;
$yr_diff = $yr_diff + 1;
}

// the results are here:

// $yr_diff --> the years between the two dates
// $mon_diff --> the month between the two dates
// $day_diff --> the days between the two dates

// ****************************************************************************

// simple output of the results
print "The difference between <b>".$base_yr."-".$base_mon."-".$base_day."</b> ";
print "and <b>".$current_yr."-".$current_mon."-".$current_day."</b> is:";
print "<br>";

// this is just to make it look nicer
$years = "years";
$days = "days";
if ($yr_diff == "1") $years = "year";
if ($day_diff == "1") $days = "day";

// here we go
print $yr_diff." ".$years.", ";
print $mon_diff." month and ";
print $day_diff." ".$days;
?>
実行結果: The difference between 2002-12-15 and 2008-7-6 is:
5 years, 6 month and 22 days

Reference: http://www.lotekk.net/ > http://www.lotekk.net/files/date-diff.php.txt



Back   Home
© 2004-2008 gn  rev. 2008/01/01 00:33:13