Maksim Morozov
Maksim Morozov

Reputation: 191

How to get day of week by date on PHP?

I get date (ex. 2015-06-12) and i want to get day of week...

For example Monday = 1

$dateprog=$_GET['dateprog'];
$dow = date("N",$dateprog);

but it's not working

Upvotes: 0

Views: 3058

Answers (4)

Kheshav Sewnundun
Kheshav Sewnundun

Reputation: 1244

The variable $dateprog is in string format which you are extracting from the

$_GET['dateprog']

and parsing it as a parameter to the date function. However, the date function takes timestamp or date format as its second parameter

string date ( string $format [, int $timestamp = time() ] )

See documentation : Date

In order to convert the string to timestamp use the strtotime function as follows:

if(isset($_GET['dateprog']))
{
    $dateprog = $_GET['dateprog'];
    $dow = date("N",strtotime($dateprog));
    echo $dow;
}

Upvotes: 3

Sunil Pachlangia
Sunil Pachlangia

Reputation: 2071

Use this

$dow = date("N",strtotime($dateprog));

Upvotes: 0

Francis
Francis

Reputation: 692

just cast it to time:

$dateprog=$_GET['dateprog'];
$dow = date("N",strtotime($dateprog));

Upvotes: 0

Sougata Bose
Sougata Bose

Reputation: 31749

Try with -

if (!empty($_GET['dateprog'])) {
    $dateprog=$_GET['dateprog'];
    $dow = date("N",strtotime($dateprog));
    echo $dow; // will print the day number of the week
}

Upvotes: 0

Related Questions