user3412075
user3412075

Reputation: 129

PHP get day N by this date format

$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

Answers (2)

Hamidreza Khakshoor
Hamidreza Khakshoor

Reputation: 74

Use strtotime() and date():

$myDate = "30-Aug-2014";
$which_day = date('N',strtotime($myDate));
echo "Which Day > " . $which_day;

Upvotes: 1

Kevin
Kevin

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

Related Questions