Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract only 'Day' value from full 'Date' string?

Tags:

date

php

I have an array of Date values in the format of 'Y-m-d'. I want to loop over the array and only extract the day ('d') of each member, but can't figure out to split it. Do I use substrings in some way?

Put simply, I have '2010-11-24', and I want to get just '24' from that. The problem being the day could be either single or double figures.

like image 837
Jack Roscoe Avatar asked Nov 25 '10 09:11

Jack Roscoe


People also ask

How do I extract just the day from a date in R?

If you just want the day as a string, you can use format(as. Date(df$x,format="%Y-%m-%d"), "%d") .


1 Answers

If you have PHP < 5.3, use strtotime():

$string = "2010-11-24"; $timestamp = strtotime($string); echo date("d", $timestamp); 

If you have PHP >= 5.3, use a DateTime based solution using createFromFormat - DateTime is the future and can deal with dates beyond 1900 and 2038:

 $string = "2010-11-24";  $date = DateTime::createFromFormat("Y-m-d", $string);  echo $date->format("d"); 
like image 155
Pekka Avatar answered Oct 05 '22 19:10

Pekka