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
Related
This question already has answers here:
Formatting an SQL timestamp with PHP
(6 answers)
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
I have a database timestamp with this format 2016-06-01 11:46:00 and I would like to convert and echo it to this format Wed. 01-06-2016.I tried a lot but no desirable results..Any ideas?
Thanks a lot, it worked!
If I want the difference between today and the past date in days or weeks I think I use date_diff.How exactly?
Simply use date and strtotime:
$time = '2016-06-01 11:46:00';
echo date("D. d-m-Y", strtotime($time)); //Wed. 01-06-2016
Updates:
$grk = array("Tet"); // complete the rest of the array
$eng = array("Wed"); // complete the rest of the array
$time = '2016-06-01 11:46:00';
$date = date("D. d-m-Y", strtotime($time));
echo str_replace($eng, $grk, $date);
$time = '2016-06-01 11:46:00';
echo date("D. d-m-Y", strtotime($time)); //Wed. 01-06-2016
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
This question already has answers here:
Add number of days to a date
(20 answers)
Closed 8 years ago.
I need to be able to count forward X number of dates into the future in PHP. For example, 14 days from today is what date? What routines in PHP can be used for this? Thanks!
This is easy with date() and strtotime():
echo date('Y-m-d', strtotime('+14 days'));
You can also use DateTime:
$date = new DateTime();
$date->modify('+14 days');
echo $date->format('Y-m-d');
Or:
$date = new DateTime('+14 days');
echo $date->format('Y-m-d');
Or as a one-liner:
echo (new DateTime('+14 days'))->format('Y-m-d');
Or with DateInterval:
$date = new DateTime();
$date->add(new DateInterval('P14D'));
echo $date->format('Y-m-d');
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to calculate the difference between two dates using PHP?
How to get difference between two dates in Year/Month/Week/Day?
i am trying to calculate years and months and days between two given dates in PHP.
i am also using timestamp of those date. is there any way to calculate years and months and
days from difference of those time stamp.
for example first date is 2 Jan, 2008. and second one is 5 July, 2012.
and result is 4 Years 5 monts and 3 days.
i am working on timestamp as date input and want to know that is there any function available which directly calculate above things by two input timestamp
You could use the DateTime object for that (please note the missing "," in the datetime constructor).
$datetime1 = new DateTime('2 Jan 2008');
$datetime2 = new DateTime('5 July 2012');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%y years %m months and %d days');
You can do this pretty easily with DateTime:
$date1 = new DateTime("2008-01-02");
$date2 = new DateTime("2012-07-05");
$diff = $date1->diff($date2);
echo "difference " . $diff->y . " years, " . $diff->m." months, ".$diff->d." days "
Documentation
You should have a look at Carbon, it's a pretty new PHP 5.3 lib on top of DateTime with a lot of usefull methods.
For Date diff:
<?php
$dtOttawa = Carbon::createFromDate(2000, 1, 1, 'America/Toronto');
$dtVancouver = Carbon::createFromDate(2013, 1, 1, 'America/Vancouver');
echo $dtOttawa->diffInHours($dtVancouver);
echo $dtOttawa->diffInDays($dtVancouver);
echo $dtOttawa->diffInMinutes($dtVancouver);
echo $dtOttawa->diffInYears($dtVancouver);
If you want Human readable diff:
$dt = Carbon::createFromDate(2011, 2, 1);
echo $dt->diffForHumans($dt->copy()->addMonth()); // 28 days before
echo $dt->diffForHumans($dt->copy()->subMonth()); // 1 month after
You can create two DateTime objects (www.php.net/datetime) from the timestamps.
When calling the diff method you get a DateInterval object, which has properties for years and months.
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);