与天比较天,并在PHP中找到下一天的日期 [英] Compare Day with day and find Next date of Day in PHP
问题描述
这是我的代码:
$ day ='星期四';
$ i = 0;
$ o_date = new DateTime(2012-09-12 20:56:43 +18小时);
$ date = date_format($ o_date,'l');
$ full = date_format($ o_date,'d-m-Y');
if($ day!= $ date){
$ date = new DateTime($ date。+1 days);
$ i ++;
}
$ order_day = new DateTime($ full。+。$ i。days); $ order_day = date_format($ order_day,'D,d M');
return $ order_day;
这是我想要做的:
1)我有一天的字符串格式(例如星期四)
2)我有一个订单日期(例如$ code> 2012-09-12 20:56:43 )
我想得到那个日期,把日子变成字符串(例如 date_format($ o_date,'l');
,以便返回星期三
,然后我想计算多少天直到订单日期之后的下一个星期四,我已经建立了一个if循环,然后我想得到原始日期,并添加 $ i
的天数累积,然后以日期格式返回日期(例如, 9月13日星期四
,但由于某种原因,我的代码无效,有人可以看到我做错了什么这里?
而不是循环,为什么不使用数字日,并计算:
$ day ='Thursday';
$ day_names = array('Sunday','Monday','Tuesdayd '星期三','星期四','星期五');
$ day_num = array_search($ day,$ day_names);
$ o_date = new DateTime(2012-09-12 20:56:43 +18小时);
$ o_day_num = $ o_date-> format('w');
$ day_diff =($ day_num - $ o_day_num)%7;
if($ day_diff == 0){$ day_diff = 7; }
$ order_day = clone $ o_date;
$ order_day-> add(new DateInterval(P。$ day_diff。D));
I'm working with dates in php and I'm a little stumped
This is my code:
$day = 'Thursday';
$i = 0;
$o_date = new DateTime("2012-09-12 20:56:43 +18 hours");
$date = date_format($o_date, 'l');
$full = date_format($o_date, 'd-m-Y');
if($day!=$date) {
$date = new DateTime($date . " +1 days");
$i++;
}
$order_day = new DateTime($full . " +".$i." days");$order_day = date_format($order_day, 'D, d M');
return $order_day;
This is what I want it to do:
1) I've got a day in string format (eg. Thursday)
2) I've got an order date (eg. 2012-09-12 20:56:43
)
I want to get that date and turn the day into a string (eg date_format($o_date, 'l');
so that will return Wednesday
, I then want to count how many days until the next Thursday after the order date, which I've built an if loop for. I then want to get the original date and add the amount of days the $i
has accumulated, then return the date in day format (eg. Thursday, 13 September
, but for some reason my code isn't working. Can someone see what I've done wrong here?
Instead of a loop, why don't you use the numeric day and calculate:
$day = 'Thursday';
$day_names = array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
$day_num = array_search($day, $day_names);
$o_date = new DateTime("2012-09-12 20:56:43 +18 hours");
$o_day_num = $o_date->format('w');
$day_diff = ($day_num - $o_day_num) % 7;
if ($day_diff == 0) { $day_diff = 7; }
$order_day = clone $o_date;
$order_day->add(new DateInterval("P".$day_diff."D"));
这篇关于与天比较天,并在PHP中找到下一天的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!