I used date('w', timestamp)
and date('w', timestamp)
to know the day, date('n', timestamp)
for months, etc.
Now I'm using datetime and I'd like to know what are the equivalent functions to get a day, a month, etc from a datetime.
PS: I know I could use UNIX_TIMESTAMP()
in a SQL query but I prefer avoiding timestamps using in my code.
$date = DateTime::createFromFormat("Y-m-d", "2068-06-15"); echo $date->format("Y");
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.
Use DateTime with DateTime::format()
$datetime = new DateTime($dateTimeString); echo $datetime->format('w');
Check out the manual: http://www.php.net/manual/en/datetime.format.php
<?php $date = new DateTime('2000-01-01'); echo $date->format('Y-m-d H:i:s'); ?>
Will output: 2000-01-01 00:00:00
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With