-1

$date と呼ばれる動的に生成された変数が PHP にあり、次のようになります。

12-02-1972
23-03-1985
18-12-1992
6-04-2001

この $date 文字列を取り、それを個別のコンポーネントに分割して、たとえば....

$day
$month
$year

これを行う最良の方法は何でしょうか?ダッシュから数字を分離するためのある種の正規表現ですか? それとももっと良い方法がありますか?

4

5 に答える 5

6

試してみてください:

list($day, $month, $year) = explode('-', '12-02-1972');
于 2013-09-30T10:27:16.427 に答える
2

DateTime形式を使用する

$date = new DateTime('2000-01-01');
echo $date->format('Y-m-d');

Yは年、mは月、dは日

だからあなたの例:

$year  = $date->format('Y');
$month = $date->format('m');
$day   = $date->format('d');

必要に応じてフォーマットする

于 2013-09-30T10:28:30.870 に答える
2

PHP 関数のexpand()を使用します。

$date = "12-02-1972";
$date = explode('-', $date);
$date[0]; // this is your day
$date[1]; // this is your month
$date[2]; // this is your year
于 2013-09-30T10:30:23.337 に答える
2
sscanf('12-02-1972', "%d-%d-%d", $day, $month, $year);
# now you have variables $day, $month and $year filled with values

ps の戻り値は文字列ではなく整数です

于 2013-09-30T10:29:43.717 に答える
1
$day = date($date, 'd');
$month = date($date, 'm');
$year = date($date, 'Y');
于 2013-09-30T10:30:46.123 に答える