$date と呼ばれる動的に生成された変数が PHP にあり、次のようになります。
12-02-1972
23-03-1985
18-12-1992
6-04-2001
この $date 文字列を取り、それを個別のコンポーネントに分割して、たとえば....
$day
$month
$year
これを行う最良の方法は何でしょうか?ダッシュから数字を分離するためのある種の正規表現ですか? それとももっと良い方法がありますか?
試してみてください:
list($day, $month, $year) = explode('-', '12-02-1972');
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
sscanf('12-02-1972', "%d-%d-%d", $day, $month, $year);
# now you have variables $day, $month and $year filled with values
ps の戻り値は文字列ではなく整数です
$day = date($date, 'd');
$month = date($date, 'm');
$year = date($date, 'Y');