【问题标题】:PHP If date is x and time is greater than x pm show A else if show BPHP 如果日期是 x 并且时间大于 x 下午显示 A 否则如果显示 B
【发布时间】:2016-11-16 01:40:13
【问题描述】:

我试图告诉 wordpress 功能根据一些复杂的因素(条件)显示发布日期

我必须展示帖子的出现风格。从 12 月 1 日开始,每天都会在网站的这个位置放置一个新帖子。但问题是加拿大(全部)、加拿大(BC)和美国的职位不同。所以这就是我本质上想要它做的事情

时区:埃德蒙顿

                            If date is from dec 1 5:00pm to dec 2 4:59pm {
                            if the user is in BC 
                            {show postid 1 
                            }else if the user is in USA
                            {show postid 2
                            }else{
                            show postid 3
                            }
                            }else
                            if date is from dec 2 5:00pm to dec 3 4:59pm{
                            if the user is in BC 
                            {show postid 4 
                            }else if the user is in USA
                            {show postid 5
                            }else{
                            show postid 6
                            }
                            }else.....
                            so on for 25 days (till December 25th)

我已经能够得到我想要的大部分内容,但我无法添加时间因素,因为我对 php 的了解不够 现在我的代码仅在日期设置和基于 IP 的情况下显示。

                            function showheroslider(){
                            date_default_timezone_set("America/Edmonton"); 

                            if (date('F, j, Y') == 'December, 1, 2016' ){

                            $userInfo = geoip_detect2_get_info_from_current_ip();
                            if ($userInfo->mostSpecificSubdivision->name == 'British Columbia'){ 

                            $adventdatepost = get_post(1123);

                            } else if ($userInfo->country->name == 'United States'){

                            $adventdatepost = get_post(1123);

                            } else {

                            $adventdatepost = get_post(1123);

                            }
                            } else 
                            if (date('F, j, Y') == 'December, 2, 2016' ){

                            $userInfo = geoip_detect2_get_info_from_current_ip();
                            if ($userInfo->mostSpecificSubdivision->name == 'British Columbia'){ 

                            $adventdatepost = get_post(1097);

                            } else if ($userInfo->country->name == 'United States'){

                            $adventdatepost = get_post(1097);

                            } else {

                            $adventdatepost = get_post(1097);

                            }
                            } else
                            if (date('F, j, Y') == 'December, 3, 2016' ){

                            $userInfo = geoip_detect2_get_info_from_current_ip();
                            if ($userInfo->mostSpecificSubdivision->name == 'British Columbia'){ 

                            $adventdatepost = get_post(1206);

                            } else if ($userInfo->country->name == 'United States'){

                            $adventdatepost = get_post(1204);

                            } else {

                            $adventdatepost = get_post(1206);

                            }else .......

总结问题是: 1.而不是仅仅在日期上,我需要它根据日期和时间显示 2. 如果这不是太复杂,我希望实际上能够编辑它以在每个月的几天而不是仅仅在 12 月工作。 (意思是每月的第一天下午 5 点到每月的第二天下午 4:59 显示这个 else ..)

第 1 点优先。请帮助我,因为我已经尽力做到这一点。但我不知道如何解决时间问题。

【问题讨论】:

  • 不要使用字符串,你只是让比较日期和时间变得更加困难,要么使用 unix 时间戳,要么改用 DateTime 类,然后你在两者之间进行检查
  • @Ghost 是的,但这将指定仅在 12 月 - 如果我希望每个月都基于当月的某天以及下午 5:00 到第二天下午 4:59 之间的时间,该怎么办?跨度>
  • 不管是几月,DateTime 肯定能满足这个条件

标签: php wordpress date if-statement time


【解决方案1】:

这是我发现的。 我仍然不是 100% 确定,但我认为这可以工作

if((date('j') == 25 && date('G') >= 17) || (date('j') == 26 && date('G') < 17)){ 
$adventdatepost = get_post('insert id for day 25);
}
else 
{
$adventdatepost = get_post('insert id for the post I want till 30 or 31st');
}

这告诉它一个月中的日期是 1 并且时间大于下午 5 点,或者日期是 2 并且时间小于下午 5 点...显示第 1 天的帖子

以此类推,直到 25 天。然后在第 25 天之后,它可以发布特定帖子,直到该月的下一个 1 天。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-28
    • 2017-02-28
    • 2019-06-14
    • 2014-12-29
    相关资源
    最近更新 更多