Jump to content

Recommended Posts

I am trying to get the 3 letter abbreviation for the day from a datetime value retrieved from MySQL. The datetime values are returned as strings, 'YYYY-MM-DD HH:MM:SS'

 

I am trying to make a daily weight & blood sugar page and want the abbreviated days for the x-plot (jpgraph, a way too cool toy).

 

A side question. Is there an access level that bypasses the Verification codes? I have inner ocular lens implants (too much karate in a younger life) which makes it REAL hard to see colored text on colored backgrounds. I tried the 'Listen to the letters' option which does not play on my Linux laptop. I know, whine, whine, whine :-)

 

Link to comment
https://forums.phpfreaks.com/topic/160680-solved-day-of-week-from-mysql-date/
Share on other sites

I am trying to get the 3 letter abbreviation for the day from a datetime value retrieved from MySQL. The datetime values are returned as strings, 'YYYY-MM-DD HH:MM:SS'

 

Read this - DATE_FORMAT().

 

A side question. Is there an access level that bypasses the Verification codes?

 

Sorry, verification codes for what?

Thanks, I had not thought about making a formatted query. I'll see if I can get the results of 30 days worth of data and still keep it in order.

 

<flip, another 'The letters you typed don't match the letters that were shown in the picture'. />

arg, another 'The letters you typed don't match...' message, make that 3 of them...

I just noticed this was moved from PHP to MySQL. Perhaps I did not make my question clear.

 

I query the datetime values out of MySQL via PHP and need to use PHP to display it as PHP's date('D',$timestamp) value, 3 letter abbreviation of the day of the week.

 

After failing miserably at trying to get a query out of MySQL that automagically converted my data I came up with the following

 

Now the question is, what is a more efficient way to convert a MySQL datetime value into the 3 letter abbreviation which represents the day of the week for each record returned.

 

<?php
  $mysqlDate = "2009-05-23 14:48:13";
  echo str_pad("MySQL Date: ",14," ",STR_PAD_LEFT).$mysqlDate."\n";
  list($year, $month, $day, $hour, $minute, $second) = split('[-\:]',$mysqlDate);
  $epoch = mktime($hour, $minute, $second, $month, $day, $year);
  echo str_pad("Epoch: ",14," ",STR_PAD_LEFT).$epoch."\n";
  echo str_pad("DOW: ",14," ",STR_PAD_LEFT).date('D',$epoch)."\n";
  echo mysqlDateToEpoch($mysqlDate)."\n";
  echo date('D',mysqlDateToEpoch($mysqlDate))."\n";

  function mysqlDateToEpoch($mysqlDate){
    list($year, $month, $day, $hour, $minute, $second) = split('[-\:]',$mysqlDate);
    return(mktime($hour, $minute, $second, $month, $day, $year));
  }
?>

 

The most efficient way was already given by Maq in the first reply in the thread - the mysql DATE_FORMAT() function, possibly with an alias attached to make accessing the produced value easier, IS all that you need.

 

P.S. The light-blue link he posted is to the mysql documentation for the function.

Thanks, I did go and read the section but did not see how it applied to what I need. I am not a DBA by any far stretch of the imagination and only know the most basic select statements.

 

I will go read up on 'aliases' to see what I am missing.

 

Thanks,

Ed

The most efficient way was already given by Maq in the first reply in the thread - the mysql DATE_FORMAT() function, possibly with an alias attached to make accessing the produced value easier, IS all that you need.

 

P.S. The light-blue link he posted is to the mysql documentation for the function.

 

You don't need aliases. Just follow the examples on that page and you should be fine. You don't have to be an expert to know how to follow a few examples and figure out how to put something fairly simple as this in your code. It may seem hard because you never used it before.

What was throwing me off was all the examples were using static values. A simple nudge would have helped a lot. This is what I came up with.

select left(dayname(dt),3) from measurments where dt > date_add(now(), interval -7 day) order by dt;

You don't need aliases. Just follow the examples on that page and you should be fine. You don't have to be an expert to know how to follow a few examples and figure out how to put something fairly simple as this in your code. It may seem hard because you never used it before.

What was throwing me off was all the examples were using static values. A simple nudge would have helped a lot. This is what I came up with.

select left(dayname(dt),3) from measurments where dt > date_add(now(), interval -7 day) order by dt;

 

Nice, so does that do exactly what you want?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.