2

I am a newbie to PhP/MySQL , Just learning things .

I am using Userspice User management framework for my project

In the database there is table called "users" and it has a field name "join_date" and the field entry will look like 2016-01-01 00:00:00 it is been stored as timestamp at the time of user registration .

To output that i use the following query

<?php $user->data()->join_date; ?>

The above query as expected outputs 2016-01-01 00:00:00

But my required output is January 2016 . So , i tried the following query

<?php $user->data()->join_date->format('F Y'); ?>

but the page becomes blank . I can understand that the above query has either syntax error or logically wrong. Can you please help me with this please . I might look silly asking these small things, I am just learning stuffs.

Update :

Also i tried the following query now thinking that storing in a value and doing will help but same error of blank page .

<?php 
$x = $user->data()->join_date;
$y = $x->format('F-Y');
echo $y;
?>
Raja Gopal
  • 1,845
  • 1
  • 17
  • 34

2 Answers2

1

you need to format date using strtotime function: (replace $date with your date variable)

$date = '2016-01-01 00:00:00';

echo date('F Y', strtotime($date));

output:

January 2016

Here DateTime class is better option:

$datetime = new DateTime($date);

echo $datetime->format('F Y');

Output:

January 2016

For more detail have look at http://php.net/manual/en/function.date.php and http://php.net/manual/en/class.datetime.php

Chetan Ameta
  • 7,696
  • 3
  • 29
  • 44
  • Thanks a ton mate . It worked ! . By the way may i know why u mentioned `DateTime` is a better option than `strtotime`. – Raja Gopal Mar 15 '16 at 05:21
  • 1
    have a look at http://stackoverflow.com/questions/8605563/datetime-class-vs-native-php-date-functions or you can search on google for same – Chetan Ameta Mar 15 '16 at 05:29
1

Try this

<?php
   $formatted_date = new DateTime($user->data()->join_date);
   echo $formatted_date->format('F Y');
?>
Niklesh Raut
  • 34,013
  • 16
  • 75
  • 109