I'm making an auction house where the auction ends after 1 month of the date it started.
Here's what i got so far:
$added_on = date('Y-m-d H:i:s');
$ends_on = new DateTime('NOW');
$ends_on->add(new DateInterval('P1M')); // 1 month
$ends_on->format('U');
$stringdate = $ends_on->format('Y-m-d H:i:s');
I need the auction to end 1 month after but on a week day, if possible, between 9h-18h. Any idea how to achieve this result?
You need to check the day number of the new $ends_on date i.e. $d = $ends_on->format("w"); If 0 (Sunday) add 1 more day. If 6 (Saturday) add 2 more days. The times will need a bit more as when do you decide the cut off time. If a entry is made after 1800 does it still expire on the same day or the next day?
Related
I want to get the week number from a specific date
Like so
if project start date is 01-12-2020,
and today is 14-12-2020
the current week number is 2
thanks
use this First get 1day of a date and get its week no cuz php provide year week no.
now get current date week no and subtract it you will get the current week no:
$month= (date('Y-m-01'));
$yearweek = date('W');
$monthweek = date('W',strtotime($month));
echo $yearweek - $monthweek;
You calculate how many weeks there are between two dates.
To do this, calculate the difference in days and divide that by 7.
The week number (Project week) is then the whole part plus 1.
$start = date_create('01-12-2020');
$curDate = date_create('14-12-2020');
$weekNumber = (int)($start->diff($curDate)->days/7)+1;
You should try with :
$now = Carbon::now();
echo $now->weekOfYear;
I counted number of days between two dates, which is 24 July and 25 July. It should be two days, but I got one day. Here's the code:
$begin = new \DateTime("2020-07-24");
$end = new \DateTime("2020-07-25");
$interval = \DateInterval::createFromDateString('1 day');
$period = new \DatePeriod($begin, $interval, $end);
echo count($period); // => this return 1
What I want is two days instead of one. Would it be correct if I just need to add +1 to count($period)? Is there any other way to solve this problem?
Since you have not mentioned the time the time would be defaulted to 00:00
With the calculation you provided the difference between these two dates is indeed 1 day. However if you want the count to be 2 days. then add 1 day to your result, it will always be correct.
I have some problem with the week number calculation in PHP/Mysql. here is my need
i have a start date and time ex."2014-09-27 00:00:00" saturday. So this week will be take as
firstweek (ie) 1. If the current date is 2014-10-03 12:16:11 this will be taking as second week or if the current date is 2014-10-08 09:09:12 it will shown as third week. So how can i calculate the week number for the current date from the start date.
2014-09-21 to 2014-09-27 is 1 week
2014-09-28 to 2014-10-04 is 2 week
2014-10-05 to 2014-10-11 is 3 week // It will continue
So please suggest me how can i do that?
You can do this with the DateTime object:
$datetime1 = new DateTime('2014-09-28');
$datetime2 = new DateTime('2014-10-04');
$interval = $datetime1->diff($datetime2);
// Output the difference in days, and convert to int
$days = (int) $interval->format('%d');
// Get number of full weeks by dividing days by seven,
// rounding it up, and adding one since you wanted start
// day to be week one.
$weeks = ceil($days / 7) + 1;
Here's a phiddle to demonstrate: http://phiddle.net/4
I want to generate a report based on this week and this month. How do I calculate this?
I mean: how do I get today's date and query the database based on the "... BETWEEN monday AND today"
date("N") will give you the day of the week ( 1-monday to 7-sunday )
So move back that number of days will take you back to Sunday.
BUT since we want Monday, N-1 will take you back to Monday instead
Monday 1 -(1-1) = 0 days
Tuesday 2 -(2-1) = 1 day
...
Sunday 7 - (7-1) = 6 days
date("Y-m-d", timestamp); will give the date
date("Y-m-d 00:00:00, timestamp) will give the datetime
date("Y-m-01", timestamp); will give the first day of the month
Use these in you query as required
e.g.
<?php
$month_start=date("Y-m-01");
$last_monday=date("Y-m-d", strtotime("-".(date("N")-1)." days"));
$today=date("Y-m-d");
$sql1="SELECT * FROM table WHERE my_date BETWEEN :month_start AND :today";
$sql2="SELECT * FROM table WHERE my_date BETWEEN :last_monday AND :today";
?>
I've omitted the sql as we dont know if your mysql_ mysqli_ or PDO
$date= new DateTime(date('Y-m-d'));
while(true)
{
if($date->format("w") != 1 )// 0 for sunday, 1 for monday, etc
{
//do things
$date->modify("-1 day");
}else{
return;
}
}
What is the first day of the week in mysql, Monday or Sunday??
I want to do this: (i am using php btw)
Get today date (no problem)
Display information of this week (stuck here)
Previous and next week button, to display previous and next week data (cant do anything here)
I am kinda of stuck while playing with the "date" thing. Can anyone help?
Monday.
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_weekday
WEEKDAY(date)
Returns the weekday index for date (0 = Monday, 1 = Tuesday, … 6 = Sunday).
To get details about previous or next weeks you can use
DATE_ADD(date,INTERVAL expr unit), DATE_SUB(date,INTERVAL expr unit)
eg SELECT DATE_ADD('2010-12-31 23:59:59', INTERVAL 1 WEEK);
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-add
To get the weekday in PHP you can use the following:
<?php
$datetime = new DateTime('2010-12-31 23:59:59');
$today = $datetime->format('l');
echo $today;
?>
http://www.php.net/manual/en/function.date.php