|
|
|
|
今日の日付と10日前の日付を取得する例
#今日の日付を取得する。
($sec, $min, $hour, $mday, $month, $year) = localtime(time()); $year += 1900; $month++; print "今日は" . $year . "年" . $month . "月" . $mday . "日です。\n"; #n日前の日付を取得する。 $n = 10; #10日前 #現在のタイムスタンプ(秒)から、n日分の秒数を引きます。 ($sec2, $min2, $hour2, $mday2, $month2, $year2) = localtime(time() - 24*60*60*$n); $year2 += 1900; $month2++; print $n . "日前は" . $year2 . "年" . $month2 . "月" . $mday2 . "日です。\n";
今日は2004年3月10日です。
10日前は2004年2月29日です。 |
|
|
今日の日付と10日前の日付を取得する例
<?php
//今日の日付を取得する。 list($sec, $min, $hour, $mday, $month, $year) = localtime(time()); $year += 1900; $month++; echo "今日は" . $year . "年" . $month . "月" . $mday . "日です。\n"; //n日前の日付を取得する。 $n = 10; //10日前 //現在のタイムスタンプ(秒)から、n日分の秒数を引きます。 list($sec2, $min2, $hour2, $mday2, $month2, $year2) = localtime(time() - 24*60*60*$n); $year2 += 1900; $month2++; echo $n . "日前は" . $year2 . "年" . $month2 . "月" . $mday2 . "日です。\n"; ?>
今日は2004年3月10日です。
10日前は2004年2月29日です。 |
|
|
今日の日付と10日前の日付を取得する例
import
java.util.Date;
import java.util.Locale; import java.util.Calendar; import java.util.GregorianCalendar; public class DateNDaysBefore { public static void main(String[] args) { //今日の日付を取得する。 Date date = new Date(); GregorianCalendar cal = new GregorianCalendar(Locale.JAPAN); cal.setTime(date); int year = cal.get(Calendar.YEAR); int month = cal.get(Calendar.MONTH) + 1; int mday = cal.get(Calendar.DAY_OF_MONTH); System.out.println("今日は" + year + "年" + month + "月" + mday + "日です。"); //n日前の日付を取得する。 int n = 10; //10日前 //現在のタイムスタンプ(ミリ秒)から、n日分のミリ秒を引きます。 Date date2 = new Date(date.getTime() - 24*60*60*1000*n); GregorianCalendar cal2 = new GregorianCalendar(Locale.JAPAN); cal2.setTime(date2); int year2 = cal2.get(Calendar.YEAR); int month2 = cal2.get(Calendar.MONTH) + 1; int mday2 = cal2.get(Calendar.DAY_OF_MONTH); System.out.println(n + "日前は" + year2 + "年" + month2 + "月" + mday2 + "日です。"); } }
今日は2004年3月10日です。
10日前は2004年2月29日です。 |
|
|
| ご意見箱コーナー (管理者宛てメール) |
|
このページは、あなたの参考になりましたか? |