如何在 PHP 中创建今天 MM/DD/YYYY 格式的变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1135573/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 01:11:26  来源:igfitidea点击:

How do I create a variable in PHP of today's date of MM/DD/YYYY format?

php

提问by Timothy T.

How do I create a variable in PHP of today's date of MM/DD/YYYY format?

如何在 PHP 中创建今天 MM/DD/YYYY 格式的变量?

I need to input that date as a hidden form field when someone comes onto the site. So I would need to grab today's date and convert it into that format. Thanks.

当有人访问网站时,我需要将该日期作为隐藏表单字段输入。所以我需要获取今天的日期并将其转换为该格式。谢谢。

回答by farzad

use the builtin date()function.

使用内置的date()函数。

$myDate = date('m/d/Y');

the string parameter 'm/d/Y' is the returned date pattern. m is for 2 digit months, d for 2 digit day value and Y for 4 digit year value.

字符串参数 'm/d/Y' 是返回的日期模式。m 代表 2 位数月份,d 代表 2 位数日值,Y 代表 4 位数年份值。

回答by Sadegh

$Date = date('m/d/Y');

回答by Pascal MARTIN

What about using the function date? Just have to find the right format ; 'm' for month, 'd' for day, 'Y' for year using four digits, for instance

使用函数date怎么样?只需要找到正确的格式;例如,'m' 代表月份,'d' 代表天,'Y' 代表年份,使用四位数

In your case, something like this, I guess :

在你的情况下,像这样,我猜:

date("m/d/Y")

And if you want another day than now, use the second optional parameter.

如果您想改天,请使用第二个可选参数。

回答by Kalpesh Desai

May be this one help you :)

可能是这个帮助你:)

<?php echo $todaydate = date('m/d/Y'); ?> // 04/27/2016 

<?php echo $todaydate = date('m/d/y'); ?> // 04/27/16 


<?php echo $todaydate = date('Y'); ?> // 2016 

<?php echo $todaydate = date('Y-m-d'); ?> // 2016-04-27 

回答by Imran Khan

<?php
$currentdate = date('m/d/Y');
echo "The Current Date is: $currentdate";
?>