Retrieve date, month, year from a date in php - php

I have this piece of code which gets me a field from the database:
$end_date=$row1['end_date'];
If i print it it gives me something like: 25-09-2012
What i need is to get the month value, the year and date.
something like:
$month=09;
$day=25;
$year=2012;
How can i do that?
thanks!

Using DateTime:
$date = new DateTime($row1['end_date']);
$year = $date -> format('Y');
$month = $date -> format('m');
$day = $date -> format('d');
If your timestamps are all like the one provided, keep it simple:
list($day, $month, $year) = explode('-', $row1['end_date']);

In your case, you can use the explode function like this :
// store a string containing "25-09-2012"
$end_date = $row1['end_date'];
// split "25-09-2012" into an array of three elements
$thedate = explode("-", $end_date);
// retrieve the values
$month = $thedate[0]; // 25
$day = $thedate[1]; // 09
$year = $thedate[2]; // 2012

try
[month('end_date')]
[day('end_date')]
[year('end_date')]
Or use explode and use - as the delimiter

Take a peak at this helpful tutorial describing various formatting methods and useful date functions in PHP:
Date/Time Functions
Date Formats

A. You can use DateTime
$date = DateTime::createFromFormat('d-m-Y',$row1['end_date']);
$month = $date->format("m");
$day = $date->format("d");
$year = $date->format("Y");
B. Using strtotime
$date = strtotime($row1['end_date']);
$month = date("m", $date);
$day = date("d", $date);
$year = date("Y", $date);
C. You can just sscanf scan through the string
$date = sscanf($row1['end_date'], "%d-%d-%d");
$month = $date[0] ;
$day = $date[1] ;
$year = $date[2] ;
D. Another method is using list & explode
list($day, $month, $year) = explode('-', $row1['end_date']);

Do it on a single line and format it however you would like. (Dec, December, 12) and so on with date().
list($month, $day, $year) = explode('-', date('m-d-Y', strtotime($row1['end_date'])));

$values = getdate(strtotime($row1['end_date']));
echo $values['mon']; //month
echo $values['mday']; //day
echo $values['year']; //year

Related

How to get Day,Date and Year from a date string in the format DD-MM-YYYY

I have a date string in the format DD-MM-YYYY .I need to get the day,month and year as separate strings.Is there a inbuilt function in php or should i use the string manipulation functions and write a function on my own.
EDIT:
What i have tried
$getdate = DateTime::createFromFormat("d-m-Y", $date);
echo $date->format("d");
error:
Call to a member function format() on a non-object in...
<?php
$year = date("Y", strtotime($yourDate));
$month = date("m", strtotime($yourDate));
$day = date("d", strtotime($yourDate));
?>
Edit
You can also this approach
$date = explode("-", $yourDate);
$day = $date[0];
$month = $date[1];
$year = $date[2];
This should work for you:
(With this you have day, month and year as separate variables)
Here I just explode() the string and save them with a list()
$date = "15-03-2015";
list($day, $month, $year) = explode("-", $date);
You bet! You can use the inbuilt DateTime class to parse a date, based on a format that you expect:
$format = 'd-m-Y';
$date = DateTime::createFromFormat($format, '15-02-2009');
echo "Format: $format; " . $date->format('Y-m-d H:i:s') . "\n";
Source: http://php.net/manual/en/datetime.createfromformat.php

Getting day, month, year from the string format "DD-MM-YYYY" [duplicate]

This question already has answers here:
Simple Question: How to split date (08-17-2011) into month, day, year? PHP
(5 answers)
Closed 3 years ago.
I have a date in string format DD-MM-YYYY.
date_string = "08-01-2008";
I would like to extract the day, month, year information from date_string such that;
day="08";
month="01";
year="2008";
How can this be done in php?
$string = "08-01-2208";
$date = DateTime::createFromFormat("d-m-Y", $string);
echo $date->format("d"); //day
echo $date->format("m"); //month
echo $date->format("Y"); //year
try explode()
$datearr = explode('-', $date_string);
echo $datearr[0]; //day
echo $datearr[1]; // month
echo $datearr[2]; // year
or date()
$day = date('d', strtotime($date_string));
$month = date('m', strtotime($date_string));
$year = date('Y', strtotime($date_string));
You can do it using strtotime. Try with the following.
$date=strtotime($date_string);
$month=date("F",$date);
$year=date("Y",$date);
$day=date("d",$date);
Try this
$date_string = "08-01-2008";
$splitDate = explode('-', $date_string);
$day = $splitDate[0];
$month = $splitDate[1];
$year = $splitDate[2];
$date_string = "08-01-2008";
list($day, $month, $year) = explode("-",$date_string);

PHP get the year from the variable

I usually mess this up, but I am trying to get a year from a variable $date = 2011-01-01 I am trying to get 2011 from the variable...I tried this but it didnt echo anything out...
date(strtotime("Y"), strtotime($date));
$year = date('Y', strtotime('2011-01-01'));
Or, if you don't want to convert to date, and trust your input format:
$year = explode('-', '2011-01-01')[0];
The above requires PHP 5.4, on older versions you need two lines:
$arr = explode('-', '2011-01-01');
$year = $arr[0];
Or yet, this slightly faster alternative suggested by tigrang:
$year = substr($date, 0, 4);
Or, an even faster way, as salathe suggested:
$year = (int) $date;
(everything after the first dash will be ignored, so $year will contain an integer with the year part of the date (unlike the other options, where $year would be a string).
date('Y', strtotime($date)) should do
First arg is the format, second is timestamp.
You can explode the date into a list and call each date item individually.
$date = '2012-01-01';
list($year, $month, $day) = explode('-', $date);
echo $year; // echoes 2012
May I suggest using PHP's DateTime class as an alternative:
$date = '2011-01-01';
$dateTime = new DateTime($date);
echo $dateTime->format('Y');
DateTime is all well and good, but lets go old school.
sscanf($date, '%d', $year);
// or
$year = strtok($date, '-');
// or
$year = substr($date, 0, 4);
// or
$year = (int) $date;
or, putting the sensible hat back on, you could use DateTime or correctly use date().
$datetime = new DateTime($date);
$year = $datetime->format('Y');
// or
$year = date('Y', strtotime($date));

conflict between implode function and strtotime

I have a problem in this code:
$month = $_POST['month'];
$day = $_POST['day'];
$year = $_POST['year'];
function dateImplodeFunction($year, $month, $day){
$array = array($year, $month, $day);
$date = date('Y-m-d', strtotime( implode("-", $array)));
return $date;
}
based on the code above I'm going to create a function where there are 3 inputs month, day and a year. When I input those 3 these variables will passed to this function, combine those 3 variables and use the implode function to create a format based on what date you specified. for instance let's say if I input 10/01/1989 it will echo the display 10/01/1989.
also I need to use date function together with strtotime function (refer to the code above) for database, setting my date field into date data type.
The problem here is if I input 10-01-1989, it returns/dipslays the value of 01-01-1970 why??
I figured it out that there is a conflict between strtotime and implode function due to test. I've search through google but it find none. I hope you can help me. Thanks in advance.
sorry for the bad english =P
I would just use the Date Time class.
$month = '07';
$day = '26';
$year = '2012';
$timezone = new DateTimeZone('America/New_York');
$date = new DateTime( "{$year}-{$month}-{$day}", $timezone );
print $date->format('Y-m-d H:i:s');
# Output: 2012-07-26 00:00:00
http://codepad.org/VxwDHPeU
You should use mktime :
function dateImplodeFunction($year, $month, $day)
{
return date('Y-m-d', mktime(0, 0, 0, $month, $day, $year));
}
Documentation of mktime here

problem to Get Date, month Year separately From database in Php File?

i want to separate date, month, time, hour, min ,sec getting from database, for count down timer,
in data base stored date is 2010/10/10:
me using this code :
$m=date('m',$row['Date']);
$d=date('d',$row['Date']);
output is month=31
date = 12....
If you want them separetely, you can use the explode function with list eg:
list($year, $month, $day) = explode('/', $row['Date']);
Also, you can use the strtotime function for getting individual values:
echo 'Day' . date('d', strtotime($row['Date']));
echo 'Month' . date('m', strtotime($row['Date']));
echo 'Year' . date('Y', strtotime($row['Date']));
$d = // Pull your date from the db
$date = new DateTime($d);
$day = $date->format('l'); // Creates day name
$day = $date->format('j') // Creates date number
$month = $date->format('n'); // Creates month number
$year = $date->format('Y'); // Creates year
..and so forth.
You can find formatting options here, http://php.net/manual/en/function.date.php

Categories