• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 28609
  • Last Modified:

PHP GetYear, GetMonth, & GetDay Functions

Does PHP have GetYear, GetMonth, &  GetDay Functions? For example:

$Date = 2003-3-3;
$Year = GetYear($Date); // yields 2003

and so on . . .
0
dbnewbie
Asked:
dbnewbie
1 Solution
 
laurlyCommented:
no but you can use getdate
http://www.php.net/manual/en/function.getdate.php

$today = getdate();
$month = $today['month'];
$mday = $today['mday'];
$year = $today['year'];
echo "$month $mday, $year";


laurly
0
 
us111Commented:
0
 
dbnewbieAuthor Commented:
Laurly,

Your assumption is that I am going to obtain the date from the system. Actually, I am going to get it from a database. Also, it will only contain date information (no time). Will what your suggesting work with that? The field type in the database is Date/Time.
0
 
VGRCommented:
of just, building from your code :

$Date = '2003-03-03'; // dates should be "complete"
$Year = substr($Date,0,4); // GetYear($Date); // yields 2003
$Month = substr($Date,5,2); // GetMonth($Date);
$Day = substr($Date,8,2); // GetDay($Date);

or (better and that can cope with your existing code) :
$Date = '2003-3-3';
$zarbi=explode('-',$Date);
$Year = $zarbi[0]; // GetYear($Date); // yields 2003
$Month = $zarbi[1];
$Day = $zarbi[2];
0
 
VGRCommented:
Note that a DATETIME containing no time part is supposed to be called a DATE field :D
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now