Forum Moderators: coopster

Message Too Old, No Replies

Explode Post Date

         

gbrown442

10:01 pm on Jul 27, 2011 (gmt 0)

10+ Year Member



Hello,

If I have a date stored in a variable that looks like this:

$post_date = 2010-04-22 17:56:43;


How can I break that date get the Year, Month and Day stored in individual variables?

Thanks

proexe

11:31 pm on Jul 27, 2011 (gmt 0)

10+ Year Member



One method on one line

list($y, $m, $d) = explode('-', substr($post_date, 0, 9));

returns three variables $y, $m and $d with the respective values from the date

gbrown442

5:09 am on Jul 28, 2011 (gmt 0)

10+ Year Member



When I do that it's only returning an Array, this is the full script:

<?php

define( 'BLOCK_LOAD', true );
require_once( $_SERVER['DOCUMENT_ROOT'] . '/wp-config.php' );
require_once( $_SERVER['DOCUMENT_ROOT'] . '/wp-includes/wp-db.php' );

$post_obj = $wp_query->get_queried_object();
$post_ID = $post_obj->ID;
$post_title = $post_obj->post_title;
$post_slug = $post_obj->post_name;

$post_date = $wpdb->get_results("SELECT post_date FROM $wpdb->posts WHERE post_name = '$post_slug'");

list($y, $m, $d) = explode('-', substr($post_date, 0, 9));

$new_url = 'http://www.example.com/TEAM-NAME/'.$y.'/'.$m.'/'.$d.'/'.$post_slug.'/';

Header( "HTTP/1.1 301 Moved Permanently" );
Header( "Location: ".$new_url."" );

?>


The result I got was:

http://www.example.com/TEAM-NAME/Array/thoughts-from-england/

gbrown442

5:41 am on Jul 28, 2011 (gmt 0)

10+ Year Member



Ok, so figured out that $post_date was returning the Array. So I changed the code to look like this:

$post_date = $wpdb->get_results("SELECT post_date FROM $wpdb->posts WHERE post_name = '$post_slug'");

list($y, $m, $d) = explode('-', substr($post_date[0], 0, 9));

But now I'm getting this error:

Catchable fatal error: Object of class stdClass could not be converted to string in /home/letsgetm/public_html/wp-content/themes/lgm_rantsports/single.php on line 14

penders

10:28 am on Jul 28, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Is $post_date an object with numeric properties? - Representing the rows of the result set? Each row is then an object containing the fields in the query (just 'post_date' in this case) .... I think.

proexe

11:45 am on Jul 28, 2011 (gmt 0)

10+ Year Member



So basically if the date wasn't a string formated like "2010-04-22 17:56:43" as stated, this changes your original question.

As $post_date, now appears to be an array, temporarily echo the the contents of this array in your script, so you can see exactly what it contains.

echo "<p>";
echo "<pre>post_date ";
Print_r($post_date);
echo "</pre></p>\n";

Then you'll be able to see which part / element that you need to extract.

This needs to be the starting point, before you can obtain the timestamp. So you can then split the year, month and day into variables.

gbrown442

2:24 pm on Jul 28, 2011 (gmt 0)

10+ Year Member



Figured this out late last night, needed to look like this:

$post_date = $wpdb->get_results("SELECT post_date FROM $wpdb->posts WHERE post_name = '$post_slug'");
foreach($post_date as $post_day) {
$postted_date = $post_day->post_date;
}
list($y, $m, $d) = explode('-', substr($postted_date, 0, 10));


Thanks for everyone's help!

rocknbil

3:53 pm on Jul 28, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You don't need to do any of that. Leverage the power of mysql:

list($yyyy,$mm,$dd) = $wpdb->get_results("SELECT date_format("%Y",post_date),date_format("%m",post_date),date_format("%d",post_date) FROM $wpdb->posts WHERE post_name = '$post_slug'");

date_format() [dev.mysql.com]