We all need to extract day, month and year from date provided.
For this, we often explode() the provided input and then get the required elements from the array.
There is a strong option to this.
Use substr() to get sub-string.
In Database, DATE and DATETIME fields have specified lengths.
We can use substr() in that case.
Examples:
For DATETIME:
$dt = '2015-07-20 11:23:56';
echo "<br/> Date: " . $dt;
echo "<br/> Year: " . substr($dt, 0, 4);
echo "<br/> Month: " . substr($dt, 5, 2);
echo "<br/> Day: " . substr($dt, 8, 2);
echo "<br/> Hour: " . substr($dt, 11, 2);
echo "<br/> Minute: " . substr($dt, 14, 2);
echo "<br/> Second: " . substr($dt, 17, 2);
For DATE:
$dt = '2015-07-20';
echo "<br/> Date: " . $dt;
echo "<br/> Year: " . substr($dt, 0, 4);
echo "<br/> Month: " . substr($dt, 5, 2);
echo "<br/> Day: " . substr($dt, 8, 2);
For this, we often explode() the provided input and then get the required elements from the array.
There is a strong option to this.
Use substr() to get sub-string.
In Database, DATE and DATETIME fields have specified lengths.
We can use substr() in that case.
Examples:
For DATETIME:
$dt = '2015-07-20 11:23:56';
echo "<br/> Date: " . $dt;
echo "<br/> Year: " . substr($dt, 0, 4);
echo "<br/> Month: " . substr($dt, 5, 2);
echo "<br/> Day: " . substr($dt, 8, 2);
echo "<br/> Hour: " . substr($dt, 11, 2);
echo "<br/> Minute: " . substr($dt, 14, 2);
echo "<br/> Second: " . substr($dt, 17, 2);
For DATE:
$dt = '2015-07-20';
echo "<br/> Date: " . $dt;
echo "<br/> Year: " . substr($dt, 0, 4);
echo "<br/> Month: " . substr($dt, 5, 2);
echo "<br/> Day: " . substr($dt, 8, 2);
No comments:
Post a Comment