php如何做日期操作 [英] php how to to do operation on date

查看:104
本文介绍了php如何做日期操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我想要计算日期时,我有问题。简单的例子:



我有 2013-09-01 是开始日期,我每个月有30天。我的工作我需要提醒告诉我的用户在结束月份前10天(这意味着 2013-09-20 我必须提醒消息它是10天更多在本月底)。所以每个人都有任何帮助来计算的想法。我喜欢不喜欢( +, - ,*,/ )。现在我有一些数据,如

 <?php 
date_default_timezone_set('Asia / Phnom_Penh');
$ current = time();
$ start = 1380188957;
echo'开始日期:'。 date('Y-m-d',$ start)。\\\

echo'< br />';
$ repeat = 30;
$ enddate = time()+($ repeat * 24 * 60 * 60);
echo'结束日期:'。 date('Y-m-d',$ enddate)\\\
;

感谢您的帮助。

解决方案

不是每个月都有31天,你可以通过使用 date()函数中的字符串格式参数的t 选项。

  //当前时间为unix时间戳
$ now = time();

//当月的天数
$ days_this_month = date(t,time());

//当月的最后一天作为unix时间戳;
$ end_of_month = strtotime(date(Y-m-t,time()));

//月底前十天为unix时间戳
$ ten_days = strtotime(' - 10天',$ end_of_month);

现在我们可以检查一下是否在月底前10天: / p>

  if($ now> $ ten_days){

//做某事
}


I have problem when i want calculate date. Simple Example:

I have 2013-09-01 is start date and I have 30day per month. My work i need alert tell to my user in 10 day before end month(it's mean on 2013-09-20i must alert message it's 10day more for end of this month). So every one have any idea for help to calculate it. becuese i like can't (+, -, *,/) on date. Now i am some data like

<?php
    date_default_timezone_set('Asia/Phnom_Penh');
    $current = time();
    $start = 1380188957;
    echo 'Start date: '. date('Y-m-d', $start) ."\n";
    echo '<br/>';
    $repeat = 30;
    $enddate = time() + ($repeat * 24 * 60 * 60);
    echo 'end date: '. date('Y-m-d', $enddate) ."\n";

Thanks in advent for helping.

解决方案

Not every month has 31 days, you can get the number of days in any month by using the t option for the string format param in php's date() function.

// Current time as unix timestamp   
$now = time();

// Number of days in current month
$days_this_month = date("t", time());

// Last day of the current month as a unix timestamp;
$end_of_month = strtotime(date("Y-m-t", time()));

// Ten days before the end of the month as a unix timestamp
$ten_days = strtotime('-10 days', $end_of_month);

Now we can do a check to see if it is 10 days before the end of the month:

if($now > $ten_days) {

    // Do something
}

这篇关于php如何做日期操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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