Skip to main content
October 30, 2010
Answered

Now that I have DATE_FORMAT working, I'd like to chop up the date into parts...how is this done?

  • October 30, 2010
  • 1 reply
  • 387 views

Hello All,

First off, many thanks to my friends Bregent and iPHP for helping me get my last problem sorted out, and fast.

Now, I have this query:

$sql = "SELECT page_ym, cal_dates, available, location, event,
        DATE_FORMAT(cal_dates, '%W %D') AS cdate
        FROM cal_data
        WHERE cal_data.page_ym = '$dateRef%'";

$result = $conn->query($sql) or die(mysqli_error());
$calSpec = $result->fetch_assoc();

and it works perfectly.

What I'd like to do is separate the weekday and date in the $calSpec['cdate'] string into two different items.

I have fiddled with the explode function but I don't think I'm doing it right. I'm assuming I'd explode the string, $calSpec['cdate'] and store its components in an array within the do while loop I have set up to populate the page with dates.

Is this the best approach? If so, can anyone steer me in the right direction with regards to proper explode syntax?

You are all amazing people, thank you in advance for your help!

Sincerely,

wordman

This topic has been closed for replies.
Correct answer

$sql = "SELECT page_ym, cal_dates, available, location, event,

        DATE_FORMAT(cal_dates, '%W') AS weekday,

        DATE_FORMAT(cal_dates, '%D') AS the_date
        FROM cal_data
        WHERE cal_data.page_ym = '$dateRef%'";

$result = $conn->query($sql) or die(mysqli_error());
$calSpec = $result->fetch_assoc();

echo $calSpec['weekday'];

echo $calSpec['the_date'];

Format the weekday/date separately in the query.

1 reply

Correct answer
October 31, 2010

$sql = "SELECT page_ym, cal_dates, available, location, event,

        DATE_FORMAT(cal_dates, '%W') AS weekday,

        DATE_FORMAT(cal_dates, '%D') AS the_date
        FROM cal_data
        WHERE cal_data.page_ym = '$dateRef%'";

$result = $conn->query($sql) or die(mysqli_error());
$calSpec = $result->fetch_assoc();

echo $calSpec['weekday'];

echo $calSpec['the_date'];

Format the weekday/date separately in the query.

October 31, 2010

iPHP

Wow! You know, I tried a variation of that, which was a complete guess, and of course it was absolutely wrong.

I had no idea you could stack DATE_FORMAT one after the other! I researched this for a LONG time and nothing came up like what you suggested, so thank you for this! I hope that this thread helps others who are or will soon be in my shoes. This forum is the BEST!!!

And of course, it works like a charm. Thank you.

Many incredibly sincere thanks,

wordman