Sam Blake
Sam Blake

Reputation: 47

Get Day of the week from Date PHP

I know there have been several posts about this before but i have been unable to get it to work in my case. I just keep getting errors.

I have a date in this format

mm-dd-yyyy

I want to get the day of the week from this. I have tried several methods as suggested on other posts such as converting it before getting the day but have had no luck. I either get the results as if the date I supplied was a dd-mm format or I just get the 1900 date. The function i have been using is

echo date('l', strtotime($datefield));

Please can someone give any advice. Let me know if you need any more information.

Thanks

Upvotes: 0

Views: 1927

Answers (2)

Ben Link
Ben Link

Reputation: 107

Use this code:

$string = "01-24-2016";
$date = DateTime::createFromFormat("m-d-Y", $string);

Now date is in the right format for your functions.

Upvotes: 2

mazedlx
mazedlx

Reputation: 1455

strtotime() 

expects dash separated dates to be in yyyy-mm-dd format an slash separated dates to be in mm/dd/yyyy format. Either change the original date or use Ben Link's solution.

Upvotes: 0

Related Questions