简体   繁体   中英

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?

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));

I'm not sure but try this:

<?php
$str="2015-07-30";
$explode=explode("-",$str);  
echo $explode[1];
?>
date('Y', strtotime(<date string from mysql>));
$date = '2015-07-30 19:20:34';
$year = date('Y', strtotime($date));

It should work.

$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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM