dongwen1935 2015-07-27 06:12
浏览 46
已采纳

在Wordpress中的ACF中创建两个日期的显示日期范围

I have two Advanced Custom Fields datepicker fields in Wordpress – a starting date and a finishing date. These are in the back end, so that my client can add/edit posts and give them a date range.

Dates below are given as dd/mm/yyyy (I'm Australian).

I need to compare the two dates to come up with a nice date range for display on the front end. It needs to:

  1. Check to see whether the two dates are the same, in which case it should just output one date (e.g. start date is 20/7/2015, finish date is 20/7/2015, so output is 20 July 2015).

  2. If the two dates are different but in the same month, output as dd – dd mm yyyy (e.g. start date is 10/3/2015, finish date is 25/3/2015, so output is 10 – 25 March 2015).

  3. If the two dates are in different months but the same year, output as dd mm – dd mm yyyy (e.g. start date is 18/4/2015, finish date is 3/5/2015, so output is 18 April – 3 May 2015).

  4. Finally, if the two dates are in different years, output as dd mm yyyy – dd mm yyyy (e.g. start date is 8/11/2015, finish date is 6/2/2016, so output is 8 November 2015 – 6 February 2016).

I can do 1 and 4 but lack the coding skills to manage 2 or 3.

I'm very surprised I couldn't find this question on SO, so if I've overlooked a duplicate please point me in the right direction.

Thanks.

  • 写回答

2条回答 默认 最新

  • dongwu4834 2015-07-27 07:17
    关注

    You can use the strtotime() function to convert your date string from the meta values to a timestamp, which you can then use to compare. The timestamp can then be passed to the date() function to compare the different date parts, and eventually to format the return value.

    $start_date = strtotime( '20/7/2015' );
    $finish_date = strtotime( '20/8/2015' );
    
    if ( $start_date == $finish_date ){
        // the start and finish are equal "d F Y" #1
        return date( 'd F Y', $start_date );
    } else {
        // first check to see if the year is the same since it is a larger scope
        if ( date( 'Y', $start_date ) == date( 'Y', $finish_date ) ){
            // year is the same, check the month next
            if ( date( 'M', $start_date ) == date( 'M', $finish_date ) ){
                // month is the same, use "d - D F Y", #2
                return date( 'd', $start_date ) . ' - ' .  date( 'd F Y', $finish_date );
            } else {
                // month is not the same but year is a match, use "d F - d F Y", #3
                return date( 'd F', $start_date ) . ' - ' .  date( 'd F Y', $finish_date );
            }
        } else {
            // the year is not the same, use "d F Y - d F Y", #4
            return date( 'd F Y', $start_date ) . ' - ' .  date( 'd F Y', $finish_date );
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用