2

データベースからフィールドを取得する次のコードがあります。

$end_date=$row1['end_date'];

25-09-2012 必要なのは、月の値、年、および日付を取得することです。何かのようなもの:

$month=09;
$day=25;
$year=2012;

どうやってやるの?ありがとう!

4

7 に答える 7

5

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']);
于 2012-09-25T14:27:17.930 に答える
2

あなたの場合、次のように爆発関数を使用できます。

// 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
于 2012-09-25T14:25:11.067 に答える
1

PHP でのさまざまな書式設定方法と便利な日付関数について説明しているこの役立つチュートリアルをご覧ください。

日付/時刻関数

日付形式

于 2012-09-25T14:26:01.437 に答える
1
$values = getdate(strtotime($row1['end_date']));
echo $values['mon']; //month
echo $values['mday']; //day
echo $values['year']; //year
于 2012-09-25T14:26:16.243 に答える
1

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);

sscanfC.文字列をスキャンするだけでよい

$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']);
于 2012-09-25T14:28:26.300 に答える
1

1行で実行し、好きなようにフォーマットしてください。(Dec、December、12) など、date() を使用します。

list($month, $day, $year) = explode('-', date('m-d-Y', strtotime($row1['end_date'])));
于 2012-09-25T14:35:31.420 に答える
1

試す [month('end_date')] [day('end_date')] [year('end_date')]

または、explodeと use - を区切り文字として使用します

于 2012-09-25T14:24:23.427 に答える