在页面php之间传递日期并仅显示年份
问题描述:
我一直在页面之间传递用户选择的日期,并调用整个日期而没有任何问题。现在,我只想从 fromdate和 todate中提取年份,下面的代码可以传递日期
I have been passing a user selected date between pages and calling the entire date without any problems. Now I want to ONLY take the year from the "fromdate" and "todate" I have got the code below to pass the date
<?
$fromdatePost = $_POST["report_date_from"];
$todatePost = $_POST["report_date_to"];
$fromdate = $_POST["report_date_from"] .' 00:00:00';
$fromdate = preg_replace('#(\d{2})/(\d{2})/(\d{4})\s(.*)#', '$3-$2-$1 $4', $fromdate);
$todate = $_POST["report_date_to"] .' 00:00:00';
$todate = preg_replace('#(\d{2})/(\d{2})/(\d{4})\s(.*)#', '$3-$2-$1 $4', $todate);
?>
然后我使用以下任意一个显示日期
Then I display the date by using either of the below
'" . $fromdate ."'
或
'" . $fromdate ."'
如何仅显示年份而不是整个日期?!
How do I get it to JUST display the year and not the entire date?!
感谢您的帮助!
答
您可以使用以下代码获取年份:
You can use the following code to get the year:
$year = date('Y', strtotime($fromdate));
用第一句话来解释,这里不需要使用正则表达式。
To paraphrase the first comment, regular expressions are not necessary here.