Vahid Amiri
Vahid Amiri

Reputation: 11127

Get Year/Month/Day separately from MySQL datetime in PHP

I store user register date and time as datetime in MySQL. now to do some process with that date i need to get year, month and day separately to work with.

for example:

2015-07-30 19:20:34

now I want 2015, how do I do that in PHP?

Upvotes: 0

Views: 9381

Answers (5)

Prabhjot Singh Kainth
Prabhjot Singh Kainth

Reputation: 1861

It is very simple, you can use strtotime() function of PHP like this:

$dated=$row['timestamp'];// in this case '2015-07-30 19:20:34';
$FullYear=date('Y',strtotime($dated)); //  result 2015
$FullYear=date('y',strtotime($dated));  //  result 15

Upvotes: 0

antelove
antelove

Reputation: 3358

$datetime = date_create("2015-07-30 19:20:34");

$day = date_format( $datetime, 'l'); // Thursday
$date = date_format( $datetime, 'd'); // 30
$month = date_format( $datetime, 'F'); // July
$year = date_format( $datetime, 'Y'); // 2015

function.date php
datetime.format.php

Upvotes: 0

Emad Mahouti
Emad Mahouti

Reputation: 17

date('Y', strtotime(<date string from mysql>));
$date = '2015-07-30 19:20:34';
$year = date('Y', strtotime($date));

It should work.

Upvotes: -2

WOUNDEDStevenJones
WOUNDEDStevenJones

Reputation: 5315

date('Y', strtotime(<date string from mysql>));

The strtotime function (http://php.net/manual/en/function.strtotime.php) parses your date string to a Unix timestamp, and then the date function (http://php.net/manual/en/function.date.php) outputs it in the defined format.

So you can do something like:

$date = '2015-07-30 19:20:34';
$year = date('Y', strtotime($date));

Upvotes: 4

NavidIvanian
NavidIvanian

Reputation: 312

I'm not sure but try this:

<?php
$str="2015-07-30";
$explode=explode("-",$str);  
echo $explode[1];
?>

Upvotes: 0

Related Questions