0

I have 2 values stored in a database. One is a startdate, the other an enddate.

Now when the Year, month and date of the 2 are equal. I would like the enddate to only show the time.

For example

maandag 16 juni 14:06 tot maandag 16 juni 14:15

would be

maandag 16 juni 14:06 tot 14:15.

I'm pretty new at PHP. I've tried the following statement, but i'm pretty sure it's incorrect:$

<?php if($item->startdate("Ymd") == ($item->enddate("Ymd"))): ?>

The type of the values are DateTime

3 Answers3

0

Try

if(date('Ymd',strtotime($item->startdate)) == date('Ymd',strtotime($item->enddate))) :

The date function should take almost any data format and reformat it to the format you give it i.e. 'Ymd'

PHP Manual for the date() function

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • I'm getting the error : "ErrorException [ Notice ]: A non well formed numeric value encountered" –  Jun 16 '14 at 12:48
  • Answer @kpp's question. What is the format of startdate and enddate in your database? Add this to your question, not as a comment. – RiggsFolly Jun 16 '14 at 12:49
  • Display the values of startdate and enddate like this var_dump($item); and put that info in the question also – RiggsFolly Jun 16 '14 at 12:54
0

You could also try using the substr-function, only comparing portions of the variables:

if(substr($item->startdate, 0, 10) == substr($item->enddate, 0, 10))

Here we compare the first ten characters (YYYY-MM-DD) of $item->startdate and $item->enddate.

Adam Bengtsson
  • 156
  • 2
  • 10
0
<?php if(date('Y-m-d',strtotime($item->startdate) == date('Y-m-d',strtotime($item->enddate))) : ?>
Samar Haider
  • 862
  • 10
  • 21