What Is the Easiest Way to Check If Time Is Set in Datetime Variable

What is the easiest way to check if time is set in datetime variable

I think you can use date_parse

check this: http://php.net/manual/en/function.date-parse.php

enter image description here

you can check for the minutes, hours and seconds from the resulting array :D

also you can check for errors in date.

enter image description here

check if datetime variable is today, tomorrow or yesterday

final now = DateTime.now();
final today = DateTime(now.year, now.month, now.day);
final yesterday = DateTime(now.year, now.month, now.day - 1);
final tomorrow = DateTime(now.year, now.month, now.day + 1);

final dateToCheck = ...
final aDate = DateTime(dateToCheck.year, dateToCheck.month, dateToCheck.day);
if(aDate == today) {
} else if(aDate == yesterday) {
} else(aDate == tomorrow) {

Hit: now.day - 1 and now.day + 1 works well with dates that result in a different year or month.

Checking to see if a DateTime variable has had a value assigned

The only way of having a variable which hasn't been assigned a value in C# is for it to be a local variable - in which case at compile-time you can tell that it isn't definitely assigned by trying to read from it :)

I suspect you really want Nullable<DateTime> (or DateTime? with the C# syntactic sugar) - make it null to start with and then assign a normal DateTime value (which will be converted appropriately). Then you can just compare with null (or use the HasValue property) to see whether a "real" value has been set.

PHP: How to check if a date is today, yesterday or tomorrow

First. You have mistake in using function strtotime see PHP documentation

int strtotime ( string $time [, int $now = time() ] )

You need modify your code to pass integer timestamp into this function.

Second. You use format d.m.Y H:i that includes time part. If you wish to compare only dates, you must remove time part, e.g. `$date = date("d.m.Y");``

Third. I am not sure if it works in the same way for you, but my PHP doesn't understand date format from $timestamp and returns 01.01.1970 02:00 into $match_date

$timestamp = "2014.09.02T13:34";
date('d.m.Y H:i', strtotime($timestamp)) === "01.01.1970 02:00";

You need to check if strtotime($timestamp) returns correct date string. If no, you need to specify format which is used in $timestamp variable. You can do this using one of functions date_parse_from_format or DateTime::createFromFormat

This is a work example:

$timestamp = "2014.09.02T13:34";

$today = new DateTime("today"); // This object represents current date/time with time set to midnight

$match_date = DateTime::createFromFormat( "Y.m.d\\TH:i", $timestamp );
$match_date->setTime( 0, 0, 0 ); // set time part to midnight, in order to prevent partial comparison

$diff = $today->diff( $match_date );
$diffDays = (integer)$diff->format( "%R%a" ); // Extract days count in interval

switch( $diffDays ) {
case 0:
echo "//Today";
case -1:
echo "//Yesterday";
case +1:
echo "//Tomorrow";
echo "//Sometime";

How to differentiate between DateTime, Date and Time

This classification is not part of the struct. IOW there's no built-in way to do this, so it's up to you to pick the implementation you'd like.

It has been suggested to check if the TotalSeconds == 0, which may satisfy you, imo it's a wonderful solution, but I think it should be used with caution because it is limited. Because what if you want to have a time+date that points to the date when TotalSeconds that really is == 0. This approach will turn this into just a date automatically.

I suggest that if you do not associate this time with a date, choose TimeSpan and make your life so much easier. However I assume this is not the case.

So, if the time really is associated with a date, I suggest you simply make your own type that wraps a DateTime, plus a boolean flag that will answer your question: is it just a date or date+time?.

This is obvious but I simply must say this anyways: if you do take this approach - encapsulate & hide!

If you expose DateTime as a field, an end-user might change the time of a date, expecting it to become date+time, yet the flag will not follow along. So don't just make a wrapper, make your own type that just uses DateTime internally.

PHP check if date and time has passed 15 minutes

You can convert the date to a timestamp with strtotime (which supports the MySQL date format) and then compare it to the current timestamp from time.

$dbtimestamp = strtotime($datefromdb);
if (time() - $dbtimestamp > 15 * 60) {
// 15 mins has passed

To compare the dates, you can use date to get the year/month/day from the timestamp and then compare them against the current date.

if (date("Y-m-d", $dbtimestamp) != date("Y-m-d")) {
// different date

Detect if a variable is a datetime object

You need isinstance(variable, datetime.datetime):

>>> import datetime
>>> now = datetime.datetime.now()
>>> isinstance(now, datetime.datetime)


As noticed by Davos, datetime.datetime is a subclass of datetime.date, which means that the following would also work:

>>> isinstance(now, datetime.date)

Perhaps the best approach would be just testing the type (as suggested by Davos):

>>> type(now) is datetime.date
>>> type(now) is datetime.datetime

Pandas Timestamp

One comment mentioned that in python3.7, that the original solution in this answer returns False (it works fine in python3.4). In that case, following Davos's comments, you could do following:

>>> type(now) is pandas.Timestamp

If you wanted to check whether an item was of type datetime.datetime OR pandas.Timestamp, just check for both

>>> (type(now) is datetime.datetime) or (type(now) is pandas.Timestamp)

Twig: how to check if a variable is a DateTime object

You should create a twig function


namespace AcmeBundle\Twig;

class CheckExtension extends \Twig_Extension
public function getFunctions() {
return array(
'isDateTime' => new \Twig_Function_Method($this, 'isDateTime'),

public function isDateTime($date) {
return ($date instanceof \DateTime); /* edit */

public function getName()
return 'acme_check_extension';


class: AcmeBundle\Twig\CheckExtension
- { name: twig.extension }

in your template:

{% if isDateTime(post.date) %} 
{% endif %}

Related Topics

Leave a reply