WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] subtracting two date arrays, reformat date array (8 posts)

  1. everyeurocounts
    Member
    Posted 9 months ago #

    Hi,

    I have a array of dates from wpdb->get results which print_r out as stdclassobj([post_date]=> 2013/7/10 00:00:00

    I have another string of dates generated as a sample to subtract the above dates from and return a array of available dates (i.e. days with no posts)

    so using array_diff($array2, $array1) returns a error (expects too long) which is due to the time format of $array1

    Anyone know how to convert the entire array into a different format?

    Or would there be a better way of showing dates with no bookings (post_dates on wp_posts table)?

    thanks in advance!

    global $wpdb;
    $fdates = $wpdb->get_results("SELECT post_date FROM wp_posts INNER JOIN wp_term_relationships
    		ON wp_term_relationships.object_id=wp_posts.ID
    		WHERE wp_term_relationships.term_taxonomy_id = $abcvn");
    
    function dateArray($from, $to, $value = NULL) {
        $begin = new DateTime($from);
        $end = new DateTime($to);
        $interval = DateInterval::createFromDateString('1 day');
        $days = new DatePeriod($begin, $interval, $end);
    
        $baseArray = array();
        foreach ($days as $day) {
            $dateKey = $day->format("Y/m/d");
            $baseArray[$dateKey] = $value;
        }
    
        return $baseArray;
    }
    
    $start = mktime(0,0,0,date("m"),date("d")-30,date("Y"));
    $finish = mktime(0,0,0,date("m"),date("d")+5,date("Y"));
    $datesArray = dateArray(date("Y/m/d", $start),date("Y/m/d", $finish),true);
    $availdates = array_diff(date("Y/m/d",$datesArray), date("Y/m/d", $fdates));
    print_r ($availdates);
  2. Chris
    Member
    Posted 9 months ago #

    Using the two PHP functions array_map and strtotime could be a start, I guess. E.g.

    $dates = array(
      '2013/7/10 00:00:00',
      '2013/12/10 00:00:00'
    );
    
    function convertStingDatesToTimestamps( $value ) {
    
      return strtotime( $value );
    
    }
    
    $new = array_map( 'convertStingDatesToTimestamps', $dates );
    
    var_dump( $dates, $new );
  3. everyeurocounts
    Member
    Posted 9 months ago #

    Thanks Chris,

    Tried this, got a error "expecting to be string" looking at it, it is returning an object rather than string or datestamp...i think the first thing to do is to return a array of dates from the array of objects ?

  4. Chris
    Member
    Posted 9 months ago #

    Ok, second attempt: Different wpdb method, different SELECT logic

    $fdates = $wpdb->get_col( 'SELECT UNIX_TIMESTAMP(post_date` ) ...your rest... ' );
    var_dump( $fdates );`

    fdates should now be an array like
    array[0] = string '{timestamp1}'
    array[1] = string '{timestamp2}'
    ...

  5. Chris
    Member
    Posted 9 months ago #

    Sry about the formatting hickup...

    $fdates = $wpdb->get_col( 'SELECT UNIX_TIMESTAMP( post_date ) ...your rest... ' );
    var_dump( $fdates );

    $fdates should now be an array like
    array[0] = string '{timestamp1}'
    array[1] = string '{timestamp2}'
    ...

  6. everyeurocounts
    Member
    Posted 9 months ago #

    Hi Chris,

    thanks for this. One small problem though, the unix timestamp includes time as well so it would only work for the same time and date. what prefix would you use instead of unix_timestamp for Y/m/d?

  7. Chris
    Member
    Posted 9 months ago #

    Third attempt :)

    SELECT DATE_FORMAT( post_date, '%Y/%m/%d' ) FROM ...

  8. everyeurocounts
    Member
    Posted 9 months ago #

    Hi Chris,

    Perfect, thanks for your persistance with this! :)

    Here is the updated working code (thanks to Chris). The intended method is part of a booking system... look up postdates where taxonomy id = 3 and compare to a sample array of dates. This is to be used to return a list of dates where no postdate exists and hence are not booked yet.

    $abcvn = "1"; // taxonomy id for query
    global $wpdb;
    
    $fdates = $wpdb->get_col( "SELECT DATE_FORMAT( post_date, '%Y/%m/%d' ) FROM wp_posts INNER JOIN wp_term_relationships
             ON wp_term_relationships.object_id=wp_posts.ID
             WHERE wp_term_relationships.term_taxonomy_id = $abcvn" ); 
    
    //var_dump( $fdates ); // test query
    
    date_default_timezone_set('Europe/London'); //needed to solve issues with +1day
    $dstart = "2012/07/04"; // range of sample dates
    $dend = "2012/07/08";
    
    function createDateRangeArraya($start, $end) {
    	$range = array();
    	if (is_string($start) === true) $start = strtotime($start); if (is_string($end) === true ) $end = strtotime($end);
    	if ($start > $end) return createDateRangeArray($end, $start);
    
    		do {
    		$range[] = date('Y/m/d', $start);
    		$start = strtotime("+ 1 day", $start);
    		}
    
    	while($start < $end);
    
    return $range;
    }
    
    $ddates= createDateRangeArraya($dstart, $dend);
    				//var_dump ( $ddates ); //uncomment to test $ddates
    
    $availdates = array_diff($ddates, $fdates);
    				//var_dump( $availdates ); //uncomment to test $availdates

Reply

You must log in to post.

About this Topic