How To: Get the Weekday as Text in Excel VBA
I often want to know what day of the week a date is. Excel has a function for this, but unfortunately it only returns an integer, for example the function:
=WEEKDAY("01/02/2009")
returns 1 (which represents Sunday).
This can be a bit confusing with a list of dates, so I wrote the following quick VBA function to get the day of the week as text, e.g. Sunday, Monday, etc.
The function (possibly not the most elegant method possible) is:
' function to return a text representation of the weekday of a given date
' returns the full text, which can be shortened by left(), etc
Function dayText(d As Date) As String
If Weekday(d) = 1 Then dayText = "Sunday"
If Weekday(d) = 2 Then dayText = "Monday"
If Weekday(d) = 3 Then dayText = "Tuesday"
If Weekday(d) = 4 Then dayText = "Wednesday"
If Weekday(d) = 5 Then dayText = "Thursday"
If Weekday(d) = 6 Then dayText = "Friday"
If Weekday(d) = 7 Then dayText = "Saturday"
End Function
(Enter this via the VBA Editor - press Alt+F11, then insert -> module and paste this code in).
It can then be called by:
=DAYTEXT("01/02/2009")
which returns Sunday.
This can then be shortened to one or three letters via the left() function, for example:
=LEFT(DAYTEXT("01/02/2009"),3)
returns Sun.
If you want to avoid macros, an alternative method is to use the (slightly messy) formula consisting of nested IFs, for a spreadsheet with a date in cell A1:
=IF(WEEKDAY(A1)=1, "Sunday", IF(WEEKDAY(A1)=2,"Monday",IF(WEEKDAY(A1)=3, "Tuesday", IF(WEEKDAY(A1)=4, "Wednesday", IF(WEEKDAY(A1)=5, "Thursday", IF(WEEKDAY(A1)=6, "Friday", "Saturday"))))))
Categories: VBA / Excel Tags: date, Excel, VBA, weekday
How To: PHP - Roots in PHP (Square Root, Cube Root and nth Root)
To find the nth root of a number in PHP, we can use the pow(base, power) function, for example the cube root of 27 is equal to 27 raised to the power of 1/3,
, since
. In general the nth root of x,
.
To calculate the square root of a number, we can also use the sqrt(number) function.
Example
Some example code showing how to calculate roots in PHP is below:
<?php // Square root echo sqrt(49) . "<br />"; // square root of 49 echo pow(49,1/2) . "<br />"; // alternative to square root of 49 echo pow(8,1/2). "<br />"; // square root of 8 // cube root echo pow(8,1/3). "<br />"; // cube root of 8 echo pow(27,1/3). "<br />"; // cube root of 27 // higher roots echo pow(390625,1/4). "<br />"; echo "25^4 = " . pow(25,4) . "<br />"; echo pow(1234, 1/6). "<br />"; ?>
The output from this example is as follows:
7 7 2.8284271247462 2 3 25 25^4 = 390625 3.2750594908837
HowTo: PHP - Raise a Number to the Power of Another (exponent in PHP)
You might need to raise a number to the power of another in a PHP script, for example
(sometimes written 3^5), or in general
. This may also be used with negative powers where
. Of course, you'd probably want to do this for more complicated examples!
PHP has a function built in to do this:
pow(x,n).
Examples of 'Power Of'
Here are some examples, which raise a number to the power of another and display the output. In the examples, <br /> inserts a line break to display the results neatly.
<?php
echo "3^5 = " . pow(3,5) . "<br />"; // 243
echo "2^9 = " . pow(2,9) . "<br />"; // 512
// Anything raised to the power of 0 is 1:
echo "99^0 = " . pow(99,0) . "<br />"; // 1
// Negative powers:
echo "3^-1 = 1/3 = " . pow(3,-1) . "<br />"; // 1/3 = 0.3333
echo "3^-2 = 1/(3^2) = " . pow(3,-2) . "<br />"; // 1/9 = 0.1111
echo "<br />";
// Loop through 2 raised to the 0,1,2,...,10:
for($i=0;$i<=10;$i++)
{
echo "2^" . $i . " = " . pow(2,$i) . "<br />";
}
?>
Output of the example:
3^5 = 243 2^9 = 512 99^0 = 1 3^-1 = 1/3 = 0.33333333333333 3^-2 = 1/(3^2) = 0.11111111111111 2^0 = 1 2^1 = 2 2^2 = 4 2^3 = 8 2^4 = 16 2^5 = 32 2^6 = 64 2^7 = 128 2^8 = 256 2^9 = 512 2^10 = 1024
Good luck! If you have any questions about this article then please post a comment!