i want convert integer number to date in year in php [duplicate] - php

This question already has answers here:
PHP: Convert Day of Year to Day of Month and Month
(2 answers)
Closed 1 year ago.
I want to convert the number of year to date
for example in number 76 in 2021 is 17-3
$d = 76;
$x = substr($d, 0, 4) .'-'. substr($d, 4,2) .'-'. substr($d,6,2) .' '. substr($d,8,2) .':'. substr($d,10,2) .':'. substr($d,12,2);
echo $x;

You can use add,
$date = new DateTime("2021");
$days = 76;
$date->add(new DateInterval("P${days}D"));
echo $date->format("Y-m-d H:i:s");

strtotime is the most obvious solution for your task:
$result = date('Y-m-d H:i:s', strtotime('2021-01-00 +76 day'));

The number 76 for the date 17-3 is the day of the year starting with 1. Some solutions with DateTime.
For the current year:
$dayOfYear = 76; //starting from 1
$dateTime = date_create('Jan 1 +'.($dayOfYear-1).' Days');
echo $dateTime->format('Y-m-d H:i:s'); //2021-03-17 00:00:00
Alternatively, the solution from here, changed for the day of the year from 1 and to get the time 00:00.
$dateTime = DateTime::createFromFormat('H:i:sz', "00:00:00".($dayOfYear-1));
If you want to use a fixed year:
$dateTime = date_create('2021-01-01 +'.($dayOfYear-1).' Days');

Related

Get End Date With Start Date Only Weekdays [duplicate]

This question already has answers here:
Add X Days To Date, Excluding Weekends
(5 answers)
Closed 3 years ago.
I want to find an end date of my first date.
For example my first date is
2020-01-01 04:20:18
And I want to get 20 days after this first date but only the weekdays (exclude the saturday and sunday)
I created this
$expirationDate = date_format(date_add(date_create(date("Y-m-d H:i:s",strtotime($_POST['created_at']))),date_interval_create_from_date_string("20 day")),'Y-m-d H:i:s');
The result I got is
2020-01-21 04:20:18
The end date result should be
2020-01-28 04:20:18
Because date 4,5,11,12,18,19,25,26 is saturday and sunday.
Anyone can help me?
You can use a loop and check which day of the week it is to achieve this. I've left your original $expirationDate calculation in to show the difference in the output:
$dt = date("Y-m-d H:i:s", strtotime('2020-01-01 04:20:18'));
$expirationDate = date_format(date_add(date_create($dt),date_interval_create_from_date_string("20 day")),'Y-m-d H:i:s');
// Add 20 days
for ($i = 0; $i < 20; $i++) {
$dt = date("Y-m-d H:i:s", strtotime("+1 day", strtotime($dt)));
// Is it a Sunday or Saturday?
$day = date('w', strtotime($dt));
if ($day == 0 || $day == 6)
// Deduct one from loop counter
$i--;
}
// Output
echo $dt;
echo $expirationDate;
Output:
$dt = 2020-01-29 04:20:18 (correct)
$expirationDate = 2020-01-21 04:20:18 (incorrect)
How does it work?
It's quite straight-forward:
Store the start date in $dt
Loop from 0 to 19 (for 20 days)
Add one day to $dt
If the day is a Sunday (0) or a Saturday (6), subtract 1 from the loop counter ($i)
An Alternative Solution
As an alternative, this may be greatly simplified by simply adding four weeks to the start time:
$dt = date("Y-m-d H:i:s", strtotime('2020-01-01 04:20:18'));
$dt = date("Y-m-d H:i:s", strtotime("+4 week", strtotime($dt)));
echo $dt;
However, this could only work if that start date was guaranteed to be a weekday. Otherwise you'd end up with an end date on a weekend.

Current month Minus 1 month [duplicate]

This question already has answers here:
PHP subtract 1 month from date formatted with date ('m-Y')
(11 answers)
Closed 5 years ago.
How do i properly minus 1 month for the current month ?
$current_month1 = date('m');
$current_month = $current_month1-1;
echo $current_month;
//current ouput
6
//desired output
06
check the following:
$now = new \DateTime("now");
$past = $now->modify("-1 month");
DateTime::modify docs
Also you can do it using DateInterval, the docs has example.
You can use date in combination with strtotime for this.
echo date('m', strtotime('last month')); // 06
The m operator in date will get you:
echo date('m', strtotime('now - 1 month'));
Gives 06.

How many days left till any date? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How many days until XXX date?
Currently I am using this code to determine how many days left till an expected day. But this code shows unexpected result. For example if $last_date is 26 December 2012, then I will get 0 day(s) left. But it should be 1 day(s) left. I think my problem is only with floor() function. Isn't it?
$timezone = "Asia/Dhaka";
if(function_exists('date_default_timezone_set')) date_default_timezone_set($timezone);
$now = time();
$last_date = strtotime("$year-$month-$day");
$datediff = $last_date - $now;
$day_left=floor($datediff/(60*60*24));
echo "$day_left day(s) left.";
N:B: My timezone is +6 GMT, I mean Asia/Dhaka.
As per the PHP documentation:
<?php
$year = '2012';
$month = '12';
$day = '26';
$current_date = new DateTime(date('Y-m-d'), new DateTimeZone('Asia/Dhaka'));
$end_date = new DateTime("$year-$month-$day", new DateTimeZone('Asia/Dhaka'));
$interval = $current_date->diff($end_date);
echo $interval->format('%a day(s)');
?>

Get the last day of the month? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP last day of the month
Is there any function like $date->getMonthDays() or $date->getLastDayOfMonth() in PHP to get the number of days in a given month (or the last day number)?
$start = new DateTime('2012-02-01');
$end = clone $start;
// Interval = last day of the month minus current day in $start
$interval = $start->getLastDayOfMonth() - intval($start->format('j'));
$end->add(new DateInterval('P' . $interval . 'D'));
EDIT: thanks, voted to close, it's a duplicate, sorry for asking...
The php date function gives you the number of days in the month with 't'
date("t");
See: http://php.net/manual/en/function.date.php
It's simple to get last month date
echo date("Y-m-t", strtotime("-1 month") ) ;
echo date("Y-m-1", strtotime("-1 month") ) ;
at March 3 returns
2011-02-28
2011-02-1
t gives you the total number of days in the current month. j gives you the current day of the month.
Using modify and some subtraction from format-ing the datetime, you can get to the end of the month.
$date = new DateTime();
$lastDayOfMonth = $date->modify(
sprintf('+%d days', $date->format('t') - $date->format('j'))
);

Calculate number of days remaining [duplicate]

This question already has answers here:
Finding the number of days between two dates
(34 answers)
Closed 3 years ago.
I would like to calculate the number of days remaining before a date. In my database I have a timestamp corresponding to the end date. For example Friday 30. I would like to say something like that :
7 days remaining... 6, 5, 4, etc
Can you help me please ?
$future = strtotime('21 July 2012'); //Future date.
$timefromdb = //source time
$timeleft = $future-$timefromdb;
$daysleft = round((($timeleft/24)/60)/60);
echo $daysleft;
$date1 = new DateTime("2016-01-01"); //current date or any date
$date2 = new DateTime("2016-12-31"); //Future date
$diff = $date2->diff($date1)->format("%a"); //find difference
$days = intval($diff); //rounding days
echo $days;
//it return 365 days omitting current day
$days = round((timestamp_from_database - time()) / 86400);
SELECT DATEDIFF(yourtimestamp, CURDATE()) AS days
doc ref: http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_datediff
$date1=date_create("2013-03-15");
$date2=date_create("2013-12-12");
$diff=date_diff($date1,$date2);
echo $diff->format("%R%a days");
http://php.net/manual/ro/function.date-diff.php

Categories