Jump to content

code problem


jimfog
 Share

Recommended Posts

Here is some code:

$cYear=date("Y");$timestamp1=mktime(0,0,0,0,  date("d",strtotime("now")),$cYear);$todaycalc=getdate($timestamp1);$today= $todaycalc['wday'];echo $today['wday'].'<br>';

I think it is obvious what i am trying to achieve. Nonetheless, nothing is printed in the browser.I just want to print the day of the week as a number(0-6), since today is Friday,ideally, 5 should have been printed, instead i just get blank.

Link to comment
Share on other sites

I think I've asked you before, but I don't remember you answering... must you use the functions? Why not use the DateTime class? A lot of the date related issues you've been raising, including this one, are trivial with that class.In this case,

$now = new DateTime();echo $now->format('w');

Link to comment
Share on other sites

I did not know that there was such a class. Nonetheless, i do not have the slightest idea how this class is going to help me in all the calendar related issues. It might help me in just few but not all of them. This is something i must "investigate". I think i must stick with the functions since must variables must be passed there. For your knowledge:

$timestamp1=mktime(0,0,0,date("n",strtotime($arg)), date("j",strtotime($arg)),$cYear);

I do not thing the above can be accomplished with the class you mentioned.

Edited by jimfog
Link to comment
Share on other sites

What exactly is the purpose of the line you're showing? I'm guessing getting a timestamp for the start of the current day (i.e. midnight).You can do that like this:

$midnight = new DateTime('midnight');$timestamp1 = $midnight->getTimestamp();

Link to comment
Share on other sites

What exactly is the purpose of the line you're showing? I'm guessing getting a timestamp for the start of the current day (i.e. midnight).
Ok here goes...the explanation-see the code:
$timestamp1 = mktime(0, 0, 0, date("n", strtotime($arg)), date("j", strtotime($arg)), $cYear);	 $today = getdate($timestamp1);

As you know, getdate without a timestamp gets an array of the current day,by passing $timestamp, i want to get a different day each time the user clicks a link of the calendar(prev or next week for example), where it mentions $arg a date is passed, and the calculation is made having as reference this variable. This variable changes in relation to "WHERE" in the calendar the user is(for example: if the user goes to the next week of April, the $arg will have a different value) You must be wondering WHY i want to do that. simply i want to have different code executed if $today[wday]>0 or $today[wday]==0-but that is a diff story-leave it for now.As you see I want to target specifically sunday of the date the user currently is OR the current day of the week is. Just focus why I am using the function above instead of a date class-i do not know if the same can be accomplished with a date class. That is up to you to answer since you know the class and it's features. Sorry for the lengthy-and complicated-explanation. I hope you got my point.

Link to comment
Share on other sites

i want to get a different day each time the user clicks a link of the calendar(prev or next week for example)
This is the key point you need to start from. Doing that is a simple:
$nextWeek = DateTime::createFromFormat('Y-m-d', $arg)->modify('+1 week');$arg = $nextWeek->format('Y-m-d');$nextWeekInfo = getdate($nextWeek->getTimestamp());if ($nextWeekInfo['wday'] === 0) { } elseif ($nextWeekInfo['wday'] > 0) {} else {}

Link to comment
Share on other sites

Well, i see your code but i cannot see in why exactly is better than using the functions i have posted(timestamp etc...). I do not observe any gain in efficiency or anything else-unless there is sth "i cannot see", in which case it would better to enlighten me.

Link to comment
Share on other sites

Readability for one thing. It's very obvious what this code does, whereas chaining the date functions typically ends up in some very awkward blocks of code.It's also very easy to deal with intervals, as illustrated by the use of modify(). In addition to what you see here, you could also do stuff like literally say you want "first weekday of next month" and other fancy stuff you'd generally have to do calculation for, rather than specify them as a string, and let PHP do the math.

Link to comment
Share on other sites

Readability for one thing. It's very obvious what this code does, whereas chaining the date functions typically ends up in some very awkward blocks of code.
You are right especially for the part that has to do with "awkward blocks of code".
It's also very easy to deal with intervals, as illustrated by the use of modify(). In addition to what you see here, you could also do stuff like literally say you want "first weekday of next month" and other fancy stuff you'd generally have to do calculation for, rather than specify them as a string, and let PHP do the math.
Excellent, exactly what i needed-although i really enjoy doing the math myself-but since we reached till here i need to show another piece of code(small) and tell mehow would i do it using the date class, i could do it my self, nonetheless, i just need that extra push. Here it is:
   if ((((date("n", $previousmonday) == $previousmonth)) || ((date("n", $nextsunday)) == ($nextmonth)))			 && ($today[wday] > 1) ) {	   		 echo '<li>' . date("d", $previousmonday) . $monthNames1[date("n", strtotime($newdate)) - 1] .		 '-test1-' . date("d", $nextsunday) . ' ' . $monthNames1[date("n", strtotime($newdate))] .		 '  ' . date("Y", strtotime($newdate)) . '</li>';

The code above is awkward as you see. Here is the logic(pseudocode): If the previousmonday of a week belongs to the previous month or the nextsunday of that week to the next month than echo a format like this: 26th March-1 April. I do not want to get in the detail of explaining what $today[wday] is doing there-leave it-just focus on the logic i am describing. The else statement(not shown here) tells php of course to output sth like 2-8 April-if the week the user is currently in is NOT half in the previous/next monthand half in the current month. Thanks for the help.

Link to comment
Share on other sites

Wouldn't it make more sense to simply have the date, get the Monday and Sunday of its week, and write both months if they're different (otherwise, just the one).

$now = new DateTime();//This here is required because for PHP, weeks begin with Sundays, not Mondays.//So if the day is Sunday (start of this week), we need to actually deal with the previous week.$week = $now->format('l') === 'Sunday' ? 'previous' : 'this';$monday = clone $now;$monday->modify('Monday ' . $week . ' week');$sunday = clone $now;$sunday->modify('Sunday ' . $week . ' week');echo $monday->format('jS' . ($monday->format('F') !== $sunday->format('F') ? ' F' : '')), '-', $sunday->format('jS F');

Link to comment
Share on other sites

Wouldn't it make more sense to simply have the date, get the Monday and Sunday of its week, and write both months if they're different (otherwise, just the one).
No doubt about it-I must now make the proper changes in the code, I will get back if a problem arises since for me workingwith the date class is sth completely new.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...