データベースからフィールドを取得する次のコードがあります。
$end_date=$row1['end_date'];
25-09-2012 必要なのは、月の値、年、および日付を取得することです。何かのようなもの:
$month=09;
$day=25;
$year=2012;
どうやってやるの?ありがとう!
データベースからフィールドを取得する次のコードがあります。
$end_date=$row1['end_date'];
25-09-2012 必要なのは、月の値、年、および日付を取得することです。何かのようなもの:
$month=09;
$day=25;
$year=2012;
どうやってやるの?ありがとう!
DateTime の使用:
$date = new DateTime($row1['end_date']);
$year = $date -> format('Y');
$month = $date -> format('m');
$day = $date -> format('d');
タイムスタンプがすべて提供されているものと同じである場合は、単純にしてください。
list($day, $month, $year) = explode('-', $row1['end_date']);
あなたの場合、次のように爆発関数を使用できます。
// store a string containing "25-09-2012"
$end_date = $row1['end_date'];
// split "25-09-2012" into an array of three elements
$thedate = explode("-", $end_date);
// retrieve the values
$month = $thedate[0]; // 25
$day = $thedate[1]; // 09
$year = $thedate[2]; // 2012
$values = getdate(strtotime($row1['end_date']));
echo $values['mon']; //month
echo $values['mday']; //day
echo $values['year']; //year
A. 使用できますDateTime
$date = DateTime::createFromFormat('d-m-Y',$row1['end_date']);
$month = $date->format("m");
$day = $date->format("d");
$year = $date->format("Y");
B. 使用strtotime
$date = strtotime($row1['end_date']);
$month = date("m", $date);
$day = date("d", $date);
$year = date("Y", $date);
sscanf
C.文字列をスキャンするだけでよい
$date = sscanf($row1['end_date'], "%d-%d-%d");
$month = $date[0] ;
$day = $date[1] ;
$year = $date[2] ;
D. 別の方法はlist
&を使用することですexplode
list($day, $month, $year) = explode('-', $row1['end_date']);
1行で実行し、好きなようにフォーマットしてください。(Dec、December、12) など、date() を使用します。
list($month, $day, $year) = explode('-', date('m-d-Y', strtotime($row1['end_date'])));
試す
[month('end_date')]
[day('end_date')]
[year('end_date')]
または、explodeと use - を区切り文字として使用します