Reputation: 129
$myDate = "30-Aug-2014";
$which_day = date('N',$myDate);
echo "Which Day > " . $which_day;
The return was
Which Day > 4
But result should be 6 as it was saturday :) How do I change to make my code return a 6 which is the actual answer. My date format in database is not unix time and is in varchar format of
d-M-Y
Upvotes: 0
Views: 123
Reputation: 74
Use strtotime() and date():
$myDate = "30-Aug-2014";
$which_day = date('N',strtotime($myDate));
echo "Which Day > " . $which_day;
Upvotes: 1
Reputation: 41903
You have to convert it to timestamp first using strtotime()
, then feed it into the date()
function. Exmaple:
$myDate = "30-Aug-2014";
$which_day = date('N', strtotime($myDate));
// ^
echo "Which Day > " . $which_day; // Which Day > 6
Or you can also use DateTime
:
$myDate = "30-Aug-2014";
$which_day = DateTime::createFromFormat('d-M-Y', $myDate);
echo "Which Day > " . $which_day->format('N');
Upvotes: 1