首页 > 编程知识 正文

php获得当前时间的时间戳,php年月日时间代码

时间:2023-05-04 06:46:15 阅读:173447 作者:590

使用PHP-strtotime和date,对今天的上个月和一年是如何获得的?

必须获取当前日期的上一个月和年。

但是,请参阅以下示例:

//Today is 2011-03-30

echodate(y-m-d )、strtotime )、lastmonth );

//Output:

2011-03-02

由于2月和3月的天数不同,这种行为在一定程度上可以理解。 上面例子的代码是我需要的,但是在每月的1号到28号之间只能正确地100%工作。

那么,你是如何以最优雅的方式获得上个月的andyear(25709522750660619392 )的? 这个方法适用于一年的每一天吗? 最佳解决方案将根据strtotime参数进行分析。

更新。 为了明确需求。

有代码可以获取最近几个月的统计信息,但首先显示上个月的统计信息,根据需要加载其他月的统计信息。 这是预期的目的。 因此,我想在这个月内找到加载上个月统计数据的年份。

还有识别时区的代码。 现在不太重要。 接受与strtotime兼容的字符串作为输入,初始化内部日期,并允许调整日期/时间。 也使用与strtotime兼容的字符串。

我知道这个操作只需要很少的条件和基本数学就可以完成,但相比之下,这确实很麻烦(例如,如果它正常工作的话)

echotz :3360日期(last month ) -格式(y-d ) ) )。

因此,我只有以前的月和年,与strtotime兼容。

答案(谢谢@dnagirl ) :

//Today is 2011-03-30

echodate(y-m-d ),strtotime ) firstdayoflastmonth ); //Output: 2011-02-01

Mr.basked 2019-10-15t 22:27336042 z

14个解决方案

46 votes

让我们看看strttotime类。 必须正确计算,并且日期格式必须与strttotime兼容。 这些措施包括:

$ datestring=' 2011-03-30 firstdayoflastmonth ';

$dt=date_create($datestring );

ECHO$dt-format(y-m ); //2011-02

dnagirlanswered 2019-10-15t 22336027336054 z

26 votes

如果一天本身无关,请执行以下操作:

echodate(y-m-d ),strtotime (date ) ) y-m ).'-1 month );

itroubsanswered 2019-10-15t 22:28336017 z

22 votes

找到答案了。 因为今天有第31个同样的问题。 正如有人提出的那样,那不是php中的错误,而是被期待的功能。 根据这篇文章,strtotime实际上将月设定为1,天数不变。 因此,如果今天是5月31日,就找4月31日。 这是无效的日期。 因此,这应该在4月30日,之后一天,5月1日发生。

在示例2011-03-30中,这个时间可以追溯到2月30日的一个月。 这是无效的,因为2月只有28天。 然后,在那几天(30-28=2)内,需要在2月28日(也就是3月2日)以后移动两天。

正如其他人指出的那样,获取“上个月”的最佳方法是使用strtotime或DateTime对象添加“第一天”或“最后一天”。

//Today being 2012-05-31

//allthefollowingreturn 2012-04-30

echodate(y-m-d )、strtotime )、lastdayof-1month );

echodate(y-m-d ),strtotime ) lastdayoflastmonth );

echodate _ create (last day of-1 month )-format ) ) y-m-d );

//allthefollowingreturn 2012-04-01

echodate(y-m-d )、strtotime )、第一天of-1 month );

echodate(y-m-d ),strtotime ) firstdayoflastmonth );

echodate _ create (first day of-1 month )-format ) y-m-d );

因此,在进行查询等时,可以使用它们来创建日期范围。

SeanDowney answere

d 2019-10-15T22:29:03Z

9 votes

如果您想要相对于特定日期的上一年和一个月,并且具有DateTime可用,则可以执行以下操作:

$d = new DateTime('2013-01-01', new DateTimeZone('UTC'));

$d->modify('first day of previous month');

$year = $d->format('Y'); //2012

$month = $d->format('m'); //12

Timo Huovinen answered 2019-10-15T22:29:27Z

6 votes

date('Y-m', strtotime('first day of last month'));

Marek answered 2019-10-15T22:29:44Z

5 votes

如果我正确理解了这个问题,那么您只想要上个月和所在的年份:

$month = date('m');

$year = date('Y');

$last_month = $month-1%12;

echo ($last_month==0?($year-1):$year)."-".($last_month==0?'12':$last_month);

?>

这是示例:[http://codepad.org/c99nVKG8]

Neal answered 2019-10-15T22:30:15Z

5 votes

strtotime具有第二个参数timestamp,它们使第一参数相对于第二参数。 因此,您可以执行以下操作:

date('Y-m', strtotime('-1 month', time()))

dieend answered 2019-10-15T22:30:39Z

4 votes

嗯,这不是一个人提到的错误。 这是预期的行为,因为一个月中的天数通常不同。 使用strtotime获取上个月的最简单方法可能是从本月的第一个月开始使用-1个月。

$date_string = date('Y-m', strtotime('-1 month', strtotime(date('Y-m-01'))));

dqhendricks answered 2019-10-15T22:31:04Z

2 votes

我认为您在strtotime函数中发现了一个错误。 每当我必须解决此问题时,我总是发现自己会根据月/年值进行数学运算。 尝试这样的事情:

$LastMonth = (date('n') - 1) % 12;

$Year = date('Y') - !$LastMonth;

Zach Rattner answered 2019-10-15T22:31:28Z

1 votes

也许缠绕的时间比您想要的长一些,但是为了使它更具可读性,我使用了不必要的代码。

也就是说,结果与您得到的结果相同-您想要/期望它得到什么?

//Today is whenever I want it to be.

$today = mktime(0,0,0,3,31,2011);

$hour = date("H",$today);

$minute = date("i",$today);

$second = date("s",$today);

$month = date("m",$today);

$day = date("d",$today);

$year = date("Y",$today);

echo "Today: ".date('Y-m-d', $today)."
";

echo "Recalulated: ".date("Y-m-d",mktime($hour,$minute,$second,$month-1,$day,$year));

如果您只需要月份和年份,则只需将日期设置为“ 01”,而不是“今天”:

$day = 1;

那应该给您您需要的。 您可以将小时,分钟和秒设置为零,也可以将它们设置为零。

date("Y-m",mktime(0,0,0,$month-1,1,$year);

减少很多;-)

Codecraft answered 2019-10-15T22:32:19Z

1 votes

这是因为上个月的天数少于当前月的天数。 我已经解决了这一问题,方法是先检查上个月的日期是否少于当前日期,然后根据该日期更改计算。

如果天数较少,则获取-1个月的最后一天,否则获取当前的-1个月:

if (date('d') > date('d', strtotime('last day of -1 month')))

{

$first_end = date('Y-m-d', strtotime('last day of -1 month'));

}

else

{

$first_end = date('Y-m-d', strtotime('-1 month'));

}

RJD22 answered 2019-10-15T22:32:50Z

0 votes

如果可以使用DateTime解决方案,则此代码段将返回上个月的年份和上个月的月份,从而避免在1月运行此代码时可能出现的陷阱。

function fn_LastMonthYearNumber()

{

$now = new DateTime();

$lastMonth = $now->sub(new DateInterval('P1M'));

$lm= $lastMonth->format('m');

$ly= $lastMonth->format('Y');

return array($lm,$ly);

}

zzapper answered 2019-10-15T22:33:15Z

0 votes

//return timestamp, use to format month, year as per requirement

function getMonthYear($beforeMonth = '') {

if($beforeMonth !="" && $beforeMonth >= 1) {

$date = date('Y')."-".date('m')."-15";

$timestamp_before = strtotime( $date . ' -'.$beforeMonth.' month' );

return $timestamp_before;

} else {

$time= time();

return $time;

}

}

//call function

$month_year = date("Y-m",getMonthYear(1));// last month before current month

$month_year = date("Y-m",getMonthYear(2)); // second last month before current month

Mukesh answered 2019-10-15T22:33:33Z

0 votes

function getOnemonthBefore($date){

$day = intval(date("t", strtotime("$date")));//get the last day of the month

$month_date = date("y-m-d",strtotime("$date -$day days"));//get the day 1 month before

return $month_date;

}

结果日期取决于输入月份所包含的天数。 如果输入月份为2月(28天),则2月5日为1月8日之前的28天。如果输入为5月17日,则是4月16日之前的31天。同样,如果输入为5月31日,则得出的日期将是4月30日。

注意:输入需要完整日期('y-m-d')和输出('y-m-d'),您可以修改此代码以适合您的需要。

Elijah Dadibalos answered 2019-10-15T22:34:04Z

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。