我在 php 中有一个名为 $date 的动态生成变量,它看起来像....
12-02-1972
23-03-1985
18-12-1992
6-04-2001
我想把这个 $date 字符串拆分成单独的组件,以便我最终得到例如....
$day
$month
$year
这样做的最佳方法是什么,某种正则表达式将数字与破折号分开?或者,还有更好的方法?
尝试:
list($day, $month, $year) = explode('-', '12-02-1972');
sscanf('12-02-1972', "%d-%d-%d", $day, $month, $year);
# now you have variables $day, $month and $year filled with values
ps 返回值是整数,而不是字符串
使用 PHP 函数explode();
$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
$day = date($date, 'd');
$month = date($date, 'm');
$year = date($date, 'Y');