-2

I want to print date separety like only day or year or month. like: just month not the rest of date time in sql

I do print date like below and its same format in my database

datetime CURRENT_TIMESTAMP

2018-10-27 19:09:49

Thanks for any help

  • 3
    Possible duplicate of [Convert from MySQL datetime to another format with PHP](https://stackoverflow.com/questions/136782/convert-from-mysql-datetime-to-another-format-with-php) – Sanu0786 Nov 01 '18 at 11:12
  • 1
    REad the Documentation,, http://php.net/manual/en/function.date.php – ArtisticPhoenix Nov 01 '18 at 11:21
  • My question is not about date format, I need explode function why duplicate ? @Sanu0786 –  Nov 01 '18 at 11:36
  • Thanks that link helps @ArtisticPhoenix –  Nov 01 '18 at 11:38

2 Answers2

2

you can display date,month,year using below code

for year

SELECT EXTRACT(YEAR FROM "2018-11-01 5:11:10");output:2018

for Month

SELECT EXTRACT(MONTH FROM "2017-06-15 5:11:10");output:6

for Day

SELECT EXTRACT(DAY FROM "2017-06-15 5:11:10");Otput:15
0

You can get the result to the variable and With PHP you should use an explode() function to achieve the result.

To split the given string it's better to separate date from time by 'space' character, and then use again the explode() function on the given date with a 'dash' symbol.

So it may looks like this:

$first_split = explode(' ', your_datetime_variable);
$date = $first_split[0]; //2018-10-27
$time = $first_split[1]; //19:09:49
$second_split = explode('-', $date);
$year = $second_split[0]; //2018
$month = $second_split[1]; //10
$day = $second_split[2]; //27
Marcin
  • 994
  • 2
  • 11
  • 26