Use strtotime() function to get the first day of week using PHP. This function returns the default time variable timestamp and then use date() function to convert timestamp date into understandable date. strtotime() Function: The strtotime() function returns the result in timestamp by parsing the time string.
You can use the date function. I'm using strtotime to get the timestamp to that day ; there are other solutions, like mktime , for instance.
Specifies the format of the outputted date string. The following characters can be used: d - The day of the month (from 01 to 31)
$dw = date( "w", $timestamp);
Where $dw will be 0 (for Sunday) through 6 (for Saturday) as you can see here: http://www.php.net/manual/en/function.date.php
My solution is this:
$tempDate = '2012-07-10';
echo date('l', strtotime( $tempDate));
Output is: Tuesday
$tempDate = '2012-07-10';
echo date('D', strtotime( $tempDate));
Output is: Tue
I think this is the correct answer, just change Europe/Stockholm
to the users time-zone.
$dateTime = new \DateTime(
'now',
new \DateTimeZone('Europe/Stockholm')
);
$day = $dateTime->format('N');
ISO-8601 numeric representation of the day of the week (added in PHP 5.1.0) 1 (for Monday) through 7 (for Sunday)
http://php.net/manual/en/function.date.php
For a list of supported time-zones, see http://php.net/manual/en/timezones.php
Thanks a lot guys for your quick comments.
This is what i will be using now. Posting the function here so that somebody may use it.
public function getDayOfWeek($pTimezone)
{
$userDateTimeZone = new DateTimeZone($pTimezone);
$UserDateTime = new DateTime("now", $userDateTimeZone);
$offsetSeconds = $UserDateTime->getOffset();
//echo $offsetSeconds;
return gmdate("l", time() + $offsetSeconds);
}
Report if you find any corrections.
Another quick way:
date_default_timezone_set($userTimezone);
echo date("l");
If you can get their timezone offset, you can just add it to the current timestamp and then use the gmdate function to get their local time.
// let's say they're in the timezone GMT+10
$theirOffset = 10; // $_GET['offset'] perhaps?
$offsetSeconds = $theirOffset * 3600;
echo gmdate("l", time() + $offsetSeconds);
$myTimezone = date_default_timezone_get();
date_default_timezone_set($userTimezone);
$userDay = date('l', $userTimestamp);
date_default_timezone_set($myTimezone);
This should work (didn't test it, so YMMV). It works by storing the script's current timezone, changing it to the one specified by the user, getting the day of the week from the date()
function at the specified timestamp, and then setting the script's timezone back to what it was to begin with.
You might have some adventures with timezone identifiers, though.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With