Jump to content

Problem using strtotime to convert two variables into a date


Recommended Posts

I am having troubles convert a two variables into a date format:

$OMONTH = "08";
$ODAY = 26;

$DATELONG = DATE("M d", strtotime("{$OMONTH} {ODAY}"));

echo $DATELONG . " Long DAte";

It should say Jan 26, but instead it is displaying DEC 31.

Preferably, I would want it to print out  "Tuesday January 26".

 

 

Thanks

I think that the problem is more than the missing dollar sign!

 

strtotime expects a string like

 

$todays_date = "03-07-1993";

 

Its defaults to Dec 31 or Jan 01 if it doesn't understand your date format, for example

 

echo DATE("M d", strtotime("Bunch of arse"));

is jan 01

 

From what I can gather you have to include a year.

 

And its the capital 'l' (a lower-case L) which gives you the day of the week, ie

 

echo DATE("L M d", strtotime("17-6-1963"));

 

 

 

 

Thank you.

 

What would the best way for me to take a date (ex: 012510) and convert it into three sepereate variables:

 

$month, $day, $dayoftheweek.

So the for the given example,

Month will = Janurary

Day will = 25

Day of week will = Monday.

 

I would need it as three seperate variables.

 

Thanks

The two lines below extract the day, month and year from your example date using sscanf and gets the day of the week using those values and strtotime/date.  It's just one example of getting what you wanted. :shy:

 

sscanf('012510', '%02d%02d%02d', $day, $month, $year);
$dayoftheweek = date('l', strtotime("20$year-$month-$day"));

var_dump($month, $day, $dayoftheweek);

In the first line, the $month and $day variables are in the wrong places (switch them around) since obviously there is no month 25! (Sorry)  This was a very simple mis-type and something you really should have been able to figure out yourself, considering you knew the input given was in ddmmyy format.  As for it "no matter what" outputting Wednesday, I've no idea how you got it to do that.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.