Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php var to year and month

Tags:

date

php

$date ='20101015';

how to convert to $year = 2010,$month = 10, $day =15

thanks

like image 420
BRAVO Avatar asked Oct 28 '25 08:10

BRAVO


1 Answers

You can use the PHP substring function substr as:

$year  = substr($date,0,4);  # extract 4 char starting at position 0.
$month = substr($date,4,2);  # extract 2 char starting at position 4.
$day   = substr($date,6);    # extract all char starting at position 6 till end.

If your original string as leading or trailing spaces this would fail, so its better feed substr trimmed input as. So before you call substr you can do:

$date = trim($date);
like image 58
codaddict Avatar answered Oct 29 '25 22:10

codaddict