如何在php中回应一月一号和十五号 [英] How to echo 1st and 15th of the month in php

查看:119
本文介绍了如何在php中回应一月一号和十五号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要回应每月Jan-Dec在PHP中的第1和第15个。我知道如何添加日子使用

I need to echo the 1st and 15th of every month Jan-Dec in PHP. I know how to add days using

$date1 = $_POST['previous_pay_date'];
  $date2 = date('M j, Y', strtotime($date1 . " + 7 day"));
  $date3 = date('M j, Y', strtotime($date2 . " + 7 day"));
  $date4 = date('M j, Y', strtotime($date3 . " + 7 day"));

  $date1 = $_POST['previous_pay_date'];
  $date2 = date('M j, Y', strtotime($date1 . " + 14 day"));
  $date3 = date('M j, Y', strtotime($date2 . " + 14 day"));
  $date4 = date('M j, Y', strtotime($date3 . " + 14 day"));

  $date1 = $_POST['previous_pay_date'];
  $date2 = date('M j, Y', strtotime($date1 . " + 1 month"));
  $date3 = date('M j, Y', strtotime($date2 . " + 1 month"));
  $date4 = date('M j, Y', strtotime($date3 . " + 1 month"));
  $date5 = date('M j, Y', strtotime($date4 . " + 1 month"));

但现在我只需要每月1日和15日

But now I need just the 1st and 15th of the month

推荐答案

我的解决方案,使用DateTime对象:看起来更简单,不是吗?

My solution, with DateTime objects: Seems much simpler, doesn't it?

<?php

header("Content-type: text/plain");

$date = new DateTime("2012-01-01");
echo $date->format("Y-m-d"), PHP_EOL;
$date->modify("+14 days");
echo $date->format("Y-m-d"), PHP_EOL;

while ($date->format("Y") != "2013") {
    $date->modify("first day of next month");
    echo $date->format("Y-m-d"), PHP_EOL;
    $date->modify("+14 days");
    echo $date->format("Y-m-d"), PHP_EOL;
}

这篇关于如何在php中回应一月一号和十五号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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