Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Date 1: dd/mm/yy
Date 2: dd/mm/yy
Date 3: dd/mm/yy
Say I have:
$date1 = 04/07/2014;
$date2 = 04/06/2014;
$date3 = 04/07/2014;
What would be the most efficient method to determine if date 2 is between or equal to date 1 and date 3 using PHP? In other words, what is the best way to check if the dates are the same, newer, or older than another date
The best way is to convert date into timestamp, then it will be easy to compare.
Try to use
$time1 = strtotime("dd/mm/yy");
$time2 = strtotime("dd/mm/yy");
Well, this really depends on what you are using this for, what I personally did (when checking dates in a MySQL database for a scheduling website I made) I used the following:
$publishDate = $row['year'] . $row['month'] . $row['day'];
$now = date("Ynj");//four number year, two number month, two number day (YYYY/MM/DD)
You could do the same, input your date as Ynj (See PHP documentation on date here)
and then compare them. This may help put this whole idea in context; this is the code I wrote for my website:
$publishDate = $row['year'] . $row['month'] . $row['day'];
$now = date("Ynj");//four number year, two number month, two number day (YYYY/MM/DD)
if($now > $publishDate)
{
//This is saying that right now, is greater than when this was added
$msg = "This date is smaller than the time now, which means it is in the past";
}
if ($now < $publishDate)
{
echo "This date is greater than the time now, which means it is in the future.";
}
if ($now == $publishDate)
{
echo "Date is the same";
}
If you add more context to your question I am sure I can help allot more! Good luck
Related
I am trying to compare to date to figure out how much time is between them, which I know how to do, date_diff(), but I want to then compare the time between the dates and if it is greater than 7 days do something and if not do something else. I think it sounds easy and I know there are probably fairly simple solutions to do so but I am just not a fan of dates and comparisons. Here is a snippet of what I got so far as it is just one case of a switch statement so the rest are basically identical.
$array = array();
$today = date("Y-m-d"); // get today's date
foreach($arrayOfObjs as $obj){
if ($obj->get("renewalDate") >= $today){
array_push($array, $obj->get("renewalDate"));
}else{
switch($obj->get("recurrencePeriod")){
case 1:
/*
* All cases follow same structure
* Build the date in format Y-m-d from renewalDate out of the obj.
* Loop through the date while it's less than today.
* After date is greater than today return date add to array
*/
$date = DateTime::createFromFormat("Y-m-d", $obj->get('renewalDate'));
while($date <= $today){
$date->add(new DateInterval('P7D'));
}
$diff = date_diff($today, $date);
if($diff->format('%a') <= 7){
$obj->renewalDate($date);
array_push($array, $obj);
}
break;
Basically, my database stores dates and those dates could be passed but it could be a reoccurring event. To calculate the next time that event would happen I check if the data in the database is before today's date and if it is then I continue to add the incremental amount (in this case 7 for a weekly reoccurring event) and compare the dates again. After the date that is incremented passes today's date I want to find out if it is within 7 days and if so add it to an array to get returned. I know... since I'm adding 7and it's within 7 days the first reoccurring event will always be within 7 days but that is not the case for monthly events or anything greater.
All cases are broken so I only included this one for simplicity. I can get date_Diff to return something like 7 or 12 or whatever the number may be but how can I check if that number is within the 7 days I want?
Thanks, I will include more information if needed to clarify any misunderstandings.
I'm not entirely sure what you are trying to do, but how about the following if you are just projecting dates forward and backwards and want to know if they are 7 days or more either way:
$today = date("Y-m-d");
$todaytime = strtotime($today);
$testdate = "2017-06-31";
$testtime = strtotime($testdate);
$back7days = strtotime("-7 days",$todaytime);
if($testtime < $back7days)
echo "X"; // do somthing if testdate was more than 7 days ago
$fwd7days = strtotime("+7 days", $todaytime);
if($testtime > $fwd7days)
echo "Y"; // do somthing if testdate is more than 7 days in future
Just make sure that you use less-than or less-than-and-equals comparators etc to handle the boundary conditions you need.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
My date string value is 1478025000. want to get year and month in the string format itself from this string,is it possible? the day should be 0 and next month and next year like that.
my desired output should be 1477852200
Try something like this:
$epoch = 1478025000;
$dt = new DateTime("#$epoch"); // convert UNIX timestamp to PHP DateTime
echo $dt->format('Y-m'); // Display as year and month: YYYY-MM
echo $dt->format('m-Y'); // Display as month and year: MM-YYYY
If you want to get just the year and month as an epoch, try something like:
$epoch = 1478025000;
$dt = new DateTime("#$epoch"); // convert UNIX timestamp to PHP DateTime
$year = $dt->format('Y');
$day = $dt->format('n');
$answer = new DateTime();
$answer->setDate($year, $month, 0);
$answer->setTime(0, 0, 0);
echo $answer->getTimestamp();
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have another question.
I get stuck by showing the tickets between two dates and two times.
I have following table fields:
Start date || ticketSDate, Start time || ticketSTime
End date || ticketEDate, End time || ticketETime
I want to show all the tickets where the End Date and End Time are bigger or equal as now
AND
Where the Start Date is less or equal as now
Can anyone help me out here?
Thanks, Benny
<?php
$result = mysqli_query($con,"SELECT * FROM ticket WHERE ticketEDate >= NOW() AND ticketETime > NOW() AND ticketSDate <= NOW() ORDER by ticketSDate ASC");
while($row = mysqli_fetch_array($result, MYSQL_BOTH)) {
echo '<tr>
<td><h8>'. $row['ticketName'] .'</h8></td><br>
<td><h4><strong>Begin:</strong> ',date("d.m.Y", strtotime ($row['ticketSDate'])),' ', date('H:i', strtotime ($row['ticketSTime'])).' hr.<h4></td>
<td><h4><strong>Einde:</strong> ',date("d.m.Y", strtotime ($row['ticketEDate'])),' ', date('H:i', strtotime ($row['ticketETime'])).' hr.<h4></td>
<td><h4>'. $row['ticketDescription'] .'</h4></td>
<td><h4>'. $row['ticketID'] .' <input name="book" type="button" value="Koop een ticket" /></h4></td>
<br><br>
</tr>';
}
?>
That's a flawed concept of thinking about time. You're saying
I want to show all the tickets where the End Date and End Time are bigger
So that means on a date which is after today but time is still not what it is right now, return false which is not reasonable at all.
So you have to use some grouped conditions. For example, End datetime is greater than now.
Now if the end date is greater than today, forget about time its meaningless.
WHERE ticketEDate > NOW() OR (ticketEDate = NOW() AND ticketETime > NOW())
Same concept for the start.
A very simple and better solution would be to store the date time as one field and then your query is a very simple one.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I need to echo 3 previous months from a string.
$monthyear = "01/2015";
OUTPUT
12/2014
11/2014
10/2014
Go ahead and try the following:
<?php
// Given string:
$monthyear = "01/2015";
// Init DateTime object:
$datetime = new DateTime();
// Create a new datetime instance, and parse given string:
$date = $datetime->createFromFormat('m/Y', $monthyear);
// Loop for 3 iterations:
for ($i = 1; $i <= 3; $i++) {
// Print current date:
echo $i . ". " . ($date->format('m/Y')) . "<br>";
// Reduce one month:
$date = date_add($date, date_interval_create_from_date_string('-1 months'));
}
?>
Yealds:
1. 01/2015
2. 12/2014
3. 11/2014
You can test this code in phpfiddle.org
Sources:
PHP createFromFormat
PHP date_add
So what you're trying to do here is get a range of dates (3 months ago to now) at a specific interval (1 month). This is easily done with something like PHP's DatePeriod class.
First you need to create a DateTime object from that string, which you can do using something like DateTime::createFromFormat. This DateTime object can be used as your ending range.
$endRange = DateTimeImmutable::createFromFormat("m/Y", $monthyear);
Second, you can subtract 3 months from that DateTime object using DateTime::sub in order to get the starting range. To do this you use a DateInterval object to specify the interval of time you wish to subtract from the DateTime object.
$startRange = $endRange->sub(new DateInterval('P3M'));
Finally, you can create your DatePeriod object using the $startRange and $endRange along with a DateInterval of 1 month and traverse the object to get the 3 desired dates.
$period = new DatePeriod($startRange, new DateInterval('P1M'), $endRange);
foreach($period as $date) {
echo $date->format("m/Y");
}
This should give you the desired dates 10/2014, 11/2014, and 12/2014.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a variable $dob which returns a date, in the following format: 1970-02-01 00:00:00
How can I use php to calculate the age of the person?
Your question is explained in detail in the PHP documentation, it bascially goes like this:
// create a datetime object for a given birthday
$birthday = new DateTime("2012-12-12");
// substract your timestamp from it
$diff = $birthday->diff($dob);
// output the difference in years
echo $diff->format('%Y');
Try this:
<?php
$age = '1970-02-01 00:00:00';
echo (int)((time()-strtotime($age))/31536000);
Output: 43, in years.
$dob = '1970-02-01 00:00:00';
$date = new DateTime($dob);
$diff = $date->diff(new DateTime);
echo $diff->format('%R%a days');
Basically stolen from here: http://uk1.php.net/manual/en/datetime.diff.php
Formatting options: http://uk1.php.net/manual/en/dateinterval.format.php
One-liner:
echo date_create('1970-02-01')->diff(date_create('today'))->y;
Demo.
i would try this
$user_dob = explode('-',$dob);
$current_year= date('Y');
$presons_age = $current_year - $user_dob[0];
This is an untested code but i feel u should get the logic.
strtotime() will convert your date into a timestamp, then subject that from time() and the result is the age in seconds.
$age_in_seconds = time() - strtotime('1970-02-01');
To display the age in years (+- 1 day), then divide by the number of seconds in a year:
echo "Age in whole years is " . floor($age_in_seconds / 60 / 60 / 24 / 365.25);