I'd be happy to help code that does not really work
I want to see if the project was published last month or not.
If yes then get a positive result.
//$project_time="2012-08-01 13:43:49";
$project_time="2012-10-02 14:05:09";
$end=mktime(0,0,0,date("m",strtotime($project_time))+1,date("d",strtotime($project_time)),d ate("y",strtotime($project_time)));
$end=date("d.m.y",$end);
$today=mktime(0,0,0,date("m"),date("d"),date("y"));
$today=date("d.m.y",$today);
echo 'Project date '.$date.'<br />';
echo 'End date '.$end.'<br />';
echo 'Today '.$today.'<br />';
if($today<$end){
echo " open<br />";
}
else{
echo " finish<br />";
}
PROJECT_TIME first gives a good result and the other not.
$ end create date based on $ PROJECT_TIME plus one month.
Variable data TOTDAY get today's date.
And the comparison I want to get an answer whether past month from PROJECTTIME
If someone has understood and can help I would be happy.
strtotime is the function you want to use. Simply use the following syntax:
$end = date('d.m.y', strtotime('+1 month', strtotime($project_time));
EDIT
What people are saying about comparing strings is correct. Don't compare strings, compare the timestamps.
Try rewriting it to something like:
$project_time = "2012-10-02 14:05:09";
$project_endtimestamp = strtotime('+1 month', strtotime($project_time));
echo 'Project date ' . $date . '<br />';
echo 'End date ' . date('d.m.y', $project_endtimestamp) . '<br />';
echo 'Today ' . date('d.m.y') . '<br />';
if (time() < $project_endtimestamp) {
echo " open<br />";
} else {
echo " finish<br />";
}
EDIT: Didn't completely understood the question. Added +1 month in strtotime call as per #Simon Germain answer.
Basic problem: You're comparing two "d.m.y" strings.
This will always fail, because PHP sees them as plain text, not as dates. Therefore, asking which one is bigger will generally give the wrong answer.
Also: Get rid of all that crazyness with the old-style date handling functions. PHP has much better ways to do that sort of thing these days.
$project_time="2012-10-02 14:05:09";
$projDate = DateTime::createFromFormat('Y-m-d H:i:s', $project_time);
$dateNow = new DateTime();
if($projDate < $dateNow) {
... do something here...
}
Related
Important note: My question is specifically about DateInterval. I am NOT interested in DateTime::modify() that is proposed in some answers. This is therefore not a duplicate.
I try to add a DateInterval of P1M (one month) to a DateTime object using a DateInterval object. Unfortunately, this seems to result in two months being added.
This code (please mind the echoes):
<?php
$date = date('m-Y');
echo $date . '<br />';
$date_subonemonth = DateTime::createFromFormat('m-Y', $date);
$date_addonemonth = DateTime::createFromFormat('m-Y', $date);
echo $date_subonemonth->format('m-Y') . '<br />';
echo $date_addonemonth->format('m-Y') . '<br />';
$one_month = new DateInterval('P1M');
$date_subonemonth = $date_subonemonth->sub($one_month)->format('m-Y');
$date_addonemonth = $date_addonemonth->add($one_month)->format('m-Y');
echo $date_subonemonth . '<br />';
echo $date_addonemonth . '<br />';
?>
Outputs the following:
01-2020
01-2020
01-2020
12-2019
03-2020
The last echo should output 02-2020 instead, it output 1-2020 when I added $one_month to it.
Why does this happen? And how can I solve this using DateInterval?
Code
$order_dispatch_date = DateTime::createFromFormat('Ymd', $inc['order_dispatch_date']);
$order_dispatch_date = $order_dispatch_date->format('Ymd');
$order_collection = date('Ymd', strtotime('next thursday', $order_dispatch_date));
$order_collection = new DateTime($order_collection);
echo '['.$order_collection->format('d/m/Y') . ' - ' . $order_dispatch_date . ' - ' . gettype($order_dispatch_date) . ' ] ';
I get the following output if the input is: 20171128
[29/08/1970 - 20171128 - string ]
My question is why is the first output showing 29th August 1970 rather than 20th November 2017?
Stop changing things back-and-forth between objects, strings and timestamps so much, especially when you're re-using the same variables. It's needlessly hard to follow.
If you've got a Ymd formatted string, and you want an equivalent string back for the proceeding Thursday, just use this:
$inc['order_dispatch_date'] = '20171128';
$order_dispatch_date = DateTime::createFromFormat('Ymd', $inc['order_dispatch_date'])->modify('next thursday');
echo $order_dispatch_date->format('Ymd');
// 20171130
See https://eval.in/909998
Basically:
$order_collection = date('Ymd', strtotime('next thursday', strtotime($order_dispatch_date)));
And that should work :)
The following code snippet:
echo date("d.m.Y-H:i:s", strtotime("01.01.2000-11:12:32"));
returns me:
01.01.1970-01:00:00
What's the right way to convert "01.01.2000-11:12:32" to time/date object, for comparing it with the current timestamp?
e.g.
if (date("d.m.Y-H:i:s") > date("d.m.Y-H:i:s", strtotime("01.01.2000-11:12:32"))) {
echo "future";
} else {
echo "past";
}
This is due to localisation. Try giving a different format, as the format matters a lot:
echo date("d.m.Y-H:i:s", strtotime("01/01/2000 11:12:32"));
echo date("d.m.Y-H:i:s", strtotime("01-01-2000 11:12:32"));
You should not have . for date and month separator.
You cannot separate date and time using -.
If you are getting the input from another source, try using str_replace:
echo date("d.m.Y-H:i:s", strtotime(str_replace(array(".", "-"), array("/", " "), "01.01.2000-11:12:32")));
Output: http://ideone.com/d19ATK
Try to replace . with -:
echo date("d.m.Y-H:i:s", strtotime(str_replace('.', '-', "01.01.2000 11:12:32")));
Also remove - between the date and time.
You can use DateTime::createFromFormat
$date = DateTime::createFromFormat('d.m.Y-H:i:s', '01.01.2000-11:12:32');
$now = new DateTime();
if ($now > $date) {
echo "future";
} else {
echo "past";
}
I'm trying to learn a little bit of php and can not understand why the 2 second IF is executing here. I have a variable $theDate set to accept a European date 4th May 2010.
$theDate = date('d-m-Y', strtotime("04-05-2010"));
echo "$theDate<br />";
if($theDate > "02-05-2010")
{
echo "Greater than!<br />";
}
if($theDate > "02-05-2011")
{
echo "Why am I showing<br />";
}
echo "Endof";
I just want to use IFs for now, no IF-Else etc. But why is the 2nd IF executing when $theDate is NOT greater than 02-05-2011,
thanks in advance,
Joe
As far as I remember its 2014. Why not use DateTime object and do why not do it right way??
$raw = '04-05-2010';
$theDate= \DateTime::createFromFormat('d-m-Y', $raw);
$raw2 = '02-05-2010';
$anotherDate = \DateTime::createFromFormat('d-m-Y', $raw2);
echo 'theDatedate: ' . $theDate->format('m/d/Y') . "<br>";
echo 'anotherDate date: ' . $anotherDate ->format('m/d/Y') . "<br>";
if ($theDate > $anotherDate ) {
echo "Greater than!<br />";
}
If you are learning php please check this resource, is worth reading.
To compare dates as string just use the UTC format YYYY-MM-DD. If you want to compare it as integer you have to convert it to seconds or compare starting with the year and then with the month and so on---
It uses string comparison. Thus it first looks at the first character, if that is greater than, true is returned. If less, false is returned. If equal, move to the next character.
You can solve this by formatting the string in reverse: Y-m-d. Or by using a built-in operator:
if($time > strtotime($text))
where $time is specified in seconds.
You are comparing strings. Convert Date to seconds and then compare,
working one for all formats etc.
$theDate = date('d-m-Y', strtotime("04-05-2010"));
echo "$theDate<br />";
if(strtotime($theDate) > strtotime("02-05-2010"))
{
echo "Greater than!<br />";
}
if(strtotime($theDate) > strtotime("02-05-2011"))
{
echo "Why am I showing<br />";
}
echo "Endof";
I am pulling a date value from a MySQL DB formatted as 01/20/13 I am calling the PHP date function on this value returned to get what day of the week it is, so 01/20/13 is today's date which is Sunday but it keeps returning the value Wednesday. I have included the code below I am new to programming so this is probably a stupid error I am overlooking.
<?php
require '../TimeCard/DB.php';
try{
$stmt = $conn->prepare('SELECT `date` FROM `timeRecords` WHERE `employeeID`= 1 ');
$stmt->execute();
} catch(PDOException $e){
echo'ERROR: ' . $e->getMessage();
}
while($row = $stmt->fetch())
{
echo date("l", $row['date']) . "<br>";
echo $row['date'] . "<br>";
}
?>
Mysql does not store dates as m/d/y it stores them as Y-m-d you're mysql database will turn "01/20/13" into 0000-00-00.
However, if you are not using the date type, and storing as a string use
strtotime($row['date'])
use strtotime on your mysql stored date, then use the date function on it
$day = date('l',strtotime($row['date']));
try with strtotime()
echo date("l", strtotime($row['date'])) . "<br>";
The second argument to PHP's date() function is an integer timestamp. You'd have better luck using DateTime, eg
$dt = DateTime::createFromFormat('m/d/y', $row['date']);
echo $dt->format('l');
This gives you the added bonus of tailoring the date parser to match your source format rather than relying on strtotime() which definitely has its quirks such as treating dates with forward-slashes (10/12/13) as US (12th October) vs dates with hyphens (10-12-13) as EU (10th December)
Example here - http://codepad.viper-7.com/iVXxA1