Generating week start and end dates with timezone offset [duplicate] - php

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Convert time and date from one time zone to another in PHP
I have two variables which show the weeks start and end date as follows:
$week = strtotime('-' . date('w') . ' days');
$start = date('Y-m-d', $week);
$end = date('Y-m-d', strtotime('+6 days', $week));
How can I offset these dates for a different timezone (e.g. PST?)

This will convert a tie in your servers timezone to the given timezone.
$datetime = new DateTime;
$newTZ = new DateTimeZone('Your/TimezoneHere');
$datetime->setTimezone($newTZ);

Related

PHP - convert midnight date into timestamp [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
I'm having trouble converting midnight date into timestamp. I'm getting via AJAX POST method timestamp (including time zone offset) from klient, so then I'm converting this to a midnight date like this:
$timestamp = 1463990400; // for example
echo date('d-m-Y H:i:s', strtotime('today', $timestamp));
This line output is: 23-05-2016 00:00:00
And I would love to convert this midnight date time into timestamp to create SQL SELECT.
Is there any solution?
Finally found way how to solve my problem with mktime() function:
$offset = date('d-m-Y H:i:s', strtotime('today', $datum_to));
$parts = preg_split('/\s+/', $offset);
$date_convert = explode("-", $parts[0]);
$time_convert = explode(":", $parts[1]);
$this_tz_str = date_default_timezone_get();
$this_tz = new DateTimeZone($this_tz_str);
$now = new DateTime("now", $this_tz);
$offset = $this_tz->getOffset($now);
$second_starttimeUTC = mktime(0, 0, 0, $date_convert[1], $date_convert[0], $date_convert[2])+$offset;

Php have function for plus date or not? [duplicate]

This question already has answers here:
Adding one day to a date
(13 answers)
Closed 6 years ago.
Php have function for plus date or not ?
normally, if i want to plus date with 5 day i usually user this code.
<?PHP
$now = date("Y-m-d");
$now_explode = explode("-", $now);
$now_year = $now_explode[0];
$now_month = $now_explode[1];
$now_date = $now_explode[2];
$now_date = $now_date + 5;
$next_five_date = $now_year."-".$now_month."-".$now_date;
echo $next_five_date;
?>
But i have some issue eg: if $now = "2016-12-31";. When i run my code. Result will be 2016-12-36 Then i have to check month, check year for date in Feb month.
It's labyrinthine, so i want to know php have general function for plus date in to date ?
Here's one of many ways to do it:
$now = date("Y-m-d"); //how to get current date
$next_five_date = date('Y-m-d', strtotime('+5 days')); //how to get date +5 days

Adding time to a date php [duplicate]

This question already has answers here:
Add number of days to a date
(20 answers)
Closed 8 years ago.
here is my current issue:
I need to add a predefined amount to a selected date. I have been using this until now:
$date=date('Y-m-d', strtotime('+7 days'));
but this returns the current date +7 days.
How can I define the current date and the modify that using this?
lets say that I have the date defined as:
$udate='2014-05-06';
I need to add 2 months to this date.
You can do,
date('Y-m-d',strtotime(date("Y-m-d", strtotime($your_date)) . " +2 months"));
You can also do using DateTime object,
$date = new DateTime($your_date);
$interval = new DateInterval('P2M');
$date->add($interval);
echo $date->format('Y-m-d')
you can use:
$date = "2014-08-25";
$newdate = strtotime ( '+2 months' , strtotime ( $date ) ) ;
$newdate = date ( 'Y-m-d' , $newdate );

find the last day/date of a WEEKOFYEAR with php [duplicate]

This question already has answers here:
How to get the first day of a given week number in PHP (multi-platform)?
(9 answers)
Closed 9 years ago.
I have a value that is the number for the weekofyear (between 1-52 ). I want to find out the date (yyyy-mm-dd) for the last day of that week.
I would prefer to do it in PHP rathe then MYSQL.
This can be easily solved with DateTime::setISODate() method:
$week = 50;
$dt = new DateTime();
$dt->setISODate($dt->format('o'), $week, 7);
echo $dt->format('Y-m-d');
demo
Or you can just create DateTime object (or unix timestamp with strtotime()) with ISO-8601 format like 2014-W50-7:
$week = 50;
$iso = sprintf("2014-W%02d-7", $week);
$dt = new DateTime($iso);
echo $dt->format('Y-m-d');
echo date('Y-m-d', strtotime($iso)); # or using strtotime()
demo

How to find the first day of the week a given date belongs to? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Get first day of week in PHP?
Given a timestamp I need to find the first day the week the timestamp belongs to.
e.g.
$format = "first day of week";
//convert to time
$time = strtotime("2011-07-01 00:00:00");
//format the time to the first day of the week
$date = strtotime($format,$time);
//Put the first day of the week into a pretty format
$weekStartDate = date("Y-m-d",$date);
The week start should equal 2011-06-27 at the moment its gone horribly wrong and equals 1970-01-01 which suggests to me the “first day of week” format is invalid.
Any ideas would be much appreciated thanks,
$time = strtotime("2011-07-01 00:00:00");
$weekStartDate = date('Y-m-d',strtotime("last Monday", $time));
Test it :
$time=[Your Request Time];
$dayofweek = date("w",strtotime($time));
if( $dayofweek != 0 )
$firstdayOfWeek = strtotime( $time . " - " . $dayofweek . " days " );
else
$firstdayOfWeek = strtotime($time);

Categories