pih*_*agy 146 php datetime date
我本周一可以通过以下方式获得:
$monday = date_create()->modify('this Monday');
Run Code Online (Sandbox Code Playgroud)
我想在本月1日轻松获得.我怎样才能做到这一点?
谢谢
Eti*_*uis 309
这是我使用的.
每月的第一天:
date('Y-m-01');
Run Code Online (Sandbox Code Playgroud)
本月最后一天:
date('Y-m-t');
Run Code Online (Sandbox Code Playgroud)
Joh*_*nde 237
需要PHP 5.3才能工作(PHP 5.3中引入了"第一天").否则上面的例子是唯一的方法:
<?php
// First day of this month
$d = new DateTime('first day of this month');
echo $d->format('jS, F Y');
// First day of a specific month
$d = new DateTime('2010-01-19');
$d->modify('first day of this month');
echo $d->format('jS, F Y');
// alternatively...
echo date_create('2010-01-19')
->modify('first day of this month')
->format('jS, F Y');
Run Code Online (Sandbox Code Playgroud)
在PHP 5.4+中,您可以这样做:
<?php
// First day of this month
echo (new DateTime('first day of this month'))->format('jS, F Y');
echo (new DateTime('2010-01-19'))
->modify('first day of this month')
->format('jS, F Y');
Run Code Online (Sandbox Code Playgroud)
如果您更喜欢简洁的方法来执行此操作,并且已经有数值的年份和月份,您可以使用date():
<?php
echo date('Y-m-01'); // first day of this month
echo date("$year-$month-01"); // first day of a month chosen by you
Run Code Online (Sandbox Code Playgroud)
kal*_*azy 31
这就是你需要的一切:
$week_start = strtotime('last Sunday', time());
$week_end = strtotime('next Sunday', time());
$month_start = strtotime('first day of this month', time());
$month_end = strtotime('last day of this month', time());
$year_start = strtotime('first day of January', time());
$year_end = strtotime('last day of December', time());
echo date('D, M jS Y', $week_start).'<br/>';
echo date('D, M jS Y', $week_end).'<br/>';
echo date('D, M jS Y', $month_start).'<br/>';
echo date('D, M jS Y', $month_end).'<br/>';
echo date('D, M jS Y', $year_start).'<br/>';
echo date('D, M jS Y', $year_end).'<br/>';
Run Code Online (Sandbox Code Playgroud)
Nik*_*ski 20
目前我正在使用此解决方案:
$firstDay = new \DateTime('first day of this month');
$lastDay = new \DateTime('last day of this month');
Run Code Online (Sandbox Code Playgroud)
我遇到的唯一问题是正在设定奇怪的时间.我的搜索界面需要正确的范围,我最终得到了这个:
$firstDay = new \DateTime('first day of this month 00:00:00');
$lastDay = new \DateTime('first day of next month 00:00:00');
Run Code Online (Sandbox Code Playgroud)
小智 18
我用一种疯狂的方法来做这个就是使用这个命令
$firstDay=date('Y-m-d',strtotime("first day of this month"));
$lastDay=date('Y-m-d',strtotime("last day of this month"));
Run Code Online (Sandbox Code Playgroud)
就这样
在PHP 5.2中,您可以使用:
<? $d = date_create();
print date_create($d->format('Y-m-1'))->format('Y-m-d') ?>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
235166 次 |
| 最近记录: |