Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php extract year/month/day/hour/minute/seconds from a date

Tags:

date

php

extract

if i have a date and i want to extract the year, the month, etc in PHP5, how should i proceed?

if i do

 $y = date('Y',$sale->end);  

it doesn't work...

like image 563
dana Avatar asked Mar 30 '11 17:03

dana


People also ask

How can I get current date in dd mm yyyy format in PHP?

date_default_timezone_set('UTC'); echo "<strong>Display current date dd/mm/yyyy format </strong>". "<br />"; echo date("d/m/Y"). "<br />"; echo "<strong>Display current date mm/dd/yyyy format</strong> "."<br />"; echo date("m/d/Y")."<br />"; echo "<strong>Display current date mm-dd-yyyy format </strong>".

What does Date () do in PHP?

Specifies the format of the outputted date string. The following characters can be used: d - The day of the month (from 01 to 31)

How do you get a date from Strtotime?

Code for converting a string to dateTime$date = strtotime ( $input ); echo date ( 'd/M/Y h:i:s' , $date );

What is Strtotime PHP?

Definition and Usage. The strtotime() function parses an English textual datetime into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT). Note: If the year is specified in a two-digit format, values between 0-69 are mapped to 2000-2069 and values between 70-100 are mapped to 1970-2000.


2 Answers

If $sale->end is a valid datestamp, pass it through strtotime() like so:

$y = date('Y', strtotime($sale->end));
like image 186
drudge Avatar answered Oct 06 '22 01:10

drudge


As jnpcl indicated, if $sale->end holds a valid datestamp you can do the following:

list($year,$month,$day,$hour,$minute,$second)=explode('-',date('Y-m-d-h-i-s',strtotime($sale->end)));
like image 35
Shad Avatar answered Oct 05 '22 23:10

Shad