在页面php之间传递日期并仅显示年份 [英] Passing date between pages php and displaying year only

查看:115
本文介绍了在页面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.

这篇关于在页面php之间传递日期并仅显示年份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆