Check Hours in a date format - php

I have to do the following,
My date have two formats as Y-m-d H:i:s and Y-m-d
My requirement is,
if date contains Hours(H), need to convert to F j, Y, g:i a
else to F j, Y
Is there any method that I can check H in Y-m-d H:i:s
Thanks in advance

Y-m-d date format is 10 chars in length and Y-m-d H:i:s date format is 19 chars in length . Based on the length you can find if it has a date part . Even if it is 19 chars in length , the time part could be 0 in which case you would disregard the time part . Along this lines should work :
// $date = "2013-09-26";
// $date = "2013-09-26 00:00:00";
$date = "2013-09-26 00:00:01";
if( strlen( $date ) == 10 )
{
// Include code to convert the date to F j, Y
echo $date , ' Does not have time part .';
}
else
{
if( strlen( $date ) == 19 and substr( $date, 10, 8 ) <> '00:00:00' )
{
// Include code to convert the date to F j, Y, g:i a
echo $date , ' Has time part which is not 0 .';
}
else
{
// Include code to convert the date to F j, Y
echo $date , ' Has time part but is 0 .';
}
}

Try this...
if(strstr($date, ' ') !== FALSE) {
$date = date('F j, Y, g:i a', strtotime($date));
} else {
$date = date('F j, Y', strtotime($date));
}

something i use when getting a date from a db that may or may not come with the time. modify as needed.
IF(
TIME( {$val['field_name']} ) = '00:00:00',
DATE_FORMAT(DATE( {$val['field_name']} ),'%D %M %Y'),
DATE_FORMAT({$val['field_name']},'%l:%i%p, %D %M %Y')
)

Its a little verbose - as #Bamar said you can just check for spaces, but this will work even if there are leading or trailing spaces in the field:
$date_format = "\s*\d\d\d\d-\d\d-\d\d\s*";
$date_time_format = "\s*\d\d\d\d-\d\d-\d\d\s\d\d:\d\d:\d\d\s*";
if(preg_match($date_time_format, $subject)){
$subject = date('F j, Y, g:i a', strtotime($subject));
}
elseif(preg_match($date_format, $subject)){
$subject = date('F j, Y', strtotime($subject));
}
else{
//formatting error - you can decide how to format.
}

Related

How to correctly compare dates in php with the same format

Trying to do a simple date comparison. Both dates are in the format of "Y F jS, g:i a". e.g. 2018 September 3rd, 9:30 am.
$today = date('Y F jS, g:i a');
$expire = file_get_contents(dirname(__FILE__).'/maintenance/end_time.php');
if($today>$expire){
unlink(dirname(__FILE__).'/maintenance/end_time.php');
}
The code I have doesn't work correctly. I also tried strototime() but that was also unsuccessful.
Try this.
$today_dt = new DateTime($today);
$expire_dt = new DateTime($expire);
if ($today_dt > $expire_dt)
{ /* Do something */ }
Update: It might possible that the date string is not supported by DateTime parser so you can initialize your dates like this.
$today_dt = DateTime::createFromFormat('Y F jS, g:i a', $today);
$expire_dt = DateTime::createFromFormat('Y F jS, g:i a', $expire);
Use strtotime
$today = time();
$expire = strtotime(file_get_contents(dirname(__FILE__).'/maintenance/end_time.php'));
if($today>$expire){
//code
}
OR
$today = date('Y F jS, g:i a');
$expire = file_get_contents(dirname(__FILE__).'/maintenance/end_time.php');
if(strtotime($today) > strtotime($expire)){
unlink(dirname(__FILE__).'/maintenance/end_time.php');
}

PHP Converting SQL date to different format showing as a string of numbers

I have a query which lists out rows from a database. I need to convert the standard SQL date format from Y-m-d H:i:s (2001-03-10 17:16:18) to F j, Y, g:i a (March 10, 2001, 5:16 pm) when echoing out the date.
To convert the date format I have
$sqlDate = $row->updateDate;
$updateDate = $phpDate = strtotime( $sqlDate );
$sqlDate = date( 'F j, Y, g:i a', $phpDate );
then echo out
echo $phpDate;
When $phpDate is echoed out it shows as a string of 10 numbers like this: 1454241452
I am not sure what those numbers are... seconds?
What am I doing wrong within the conversion of strtotime ? Or would my problem lie elswere?
Thank you for your time.
You are echoing the wrong date var, try it like this:
$sqlDate = $row->updateDate; # get date from database
$utime = strtotime($sqlDate); # this is unix time (check php.net/date)
$newdate = date( 'F j, Y, g:i a', $utime); # convert unix time to desired format
echo $newdate;
or in one line:
$newdate = date( 'F j, Y, g:i a', strtotime($row->updateDate));
echo $newdate;
Your $phpDate variable value store strtotime but not correct.
You change your code some modify like this
$sqlDate = $row->updateDate;
$updateDate = $phpDate = strtotime( $sqlDate );
$sqlDate = date( 'F j, Y, g:i a', $phpDate );
$phpDate= date( 'F j, Y, g:i a', $phpDate );
echo $phpDate;
This code working fine
this code useful for You :)

php change the string into time format

In php I have time like this
$time = '2015-06-29T16:00:00Z';
I want to convert that time like this format Tuesday, December 16, 2015 3:00 PM
For that I tried
echo date( 'jS F Y', strtotime( $time) );
but it is showing time like 1st January 1970
So can someone help me to get the actual time format as I want.
A simple DateTime class usage should suffice, just feed it into the constructor, the just use ->format and provide the desired output format:
$time = '2015-06-29T16:00:00Z';
$date = new DateTime($time);
echo $date->format('jS F Y');
Sample Output
You can use the DateTime class for better handling of dates
$time = '2015-06-29T16:00:00Z';
$dateTime = new DateTime($time);
echo $dateTime->format('l, F d, Y g:i A');
$time = '2015-06-29T16:00:00Z';
echo date( 'l, F j, Y H:i A',strtotime($time));
l, F j, Y H:i A can be re-ordered to change the output.
About date function, http://php.net/manual/en/function.date.php
Just pass proper format parameters to it.
$time = '2015-06-29T16:00:00Z';
echo date( 'l, F j, Y g:i A', strtotime( $time) );
Use preg_split:
$parts = preg_plit("/Z/",$time);
$parts = preg_split("/T/",$parts[0]);
$theDate=$parts[0];
$theTime=$parts[1];
$what_you_want=date(strtotime($theDate." ".$theTime);
Note that you can still change the format of the output.

Add 1 Week in DateTime to Create Expiry DateTime

I'm Getting Saved Date/Time from Database in Following Format,
$date_time = date("j M, Y, g:i a"); //1 Feb, 2015, 12:00 am
Looking for a solution to add 7 days in $date_time to create expiry date, Tried several solutions discused on stackoverflow but none of them is working for me,
Any help will be appriciated. Thanks
Edited
#panther answered partially worked with only Date
$date_time = date("j M, Y");
but with Date/Time
$date_time = date("j M, Y, g:i a");
adding 7 days
$end_date = date("j M, Y, g:i a", strtotime($date_time . ' + 7 days'));
will return the result "31 Dec, 1969, 12:00 am"
So I tried with only Date to calcualte the difference (Inside PHP While Loop)
<?php if ($end_date>$date_time) { ?>
Expired
<?php } else { ?>
Active
<?php } ?>
And it didn't work, either it sets all records to Active or Expired,
So I'm Back to Sqaure 1.
Note: I tried with UNIX_TIMESTAMP but the end result same, either sets all records to Active or Expired.
Try this:
$date_time = date("j M, Y, g:i a");
$end_date = date("j M, Y, g:i a", strtotime($date_time . ' + 7 days'));
Using DateTime is also a good option:
$date_str = "..."; // from database
$date_format = "j M, Y, g:i a";
$expire_date = DateTime::createFromFormat($date_format, $date_str);
$expire_date = $expire_date->add(DateInterval::createFromDateString('+7 days'));
and if you need it formated back to string:
$expire_date->format($date_format);
You could try using PHPs DateTime Object to get the current Time
$date_time_obj = new \DateTime();
$date_time = $date_time->format('j M, Y, g:i a');
And then use the add() method to get your expiry time
$end_date_obj = clone $date_time_obj;
$end_date_obj = $end_date_obj->add(new \DateTimeInterval('P7D'));
$end_date = $end_date_obj->format('j M, Y, g:i a');
A little more complicated than the other answers, i know :) But maybe also a little more helpfull in the future!
Cheers

php date is wrong?

Let's say I have this:
$a11 = date("F j, Y, g:i a", $a['date']);
$newTime = date($a['date'], strtotime('+3 hour'));
$b11 = date("F j, Y, g:i a", $newTime);
echo $a11 . " AND " . $b11;
I know $a['date'] is right because I get: March 22, 2011, 10:22 pm. However, the echo produces: March 22, 2011, 10:22 pm AND March 22, 2011, 10:22 pm when clearly the second part is suppose to be three hours ahead.
What am I doing wrong?
Don't you want:
$newTime = strtotime( '+3 hours',$a['date'] );
$b11 = date("F j, Y, g:i a", $newTime );
It seems you provide the wrong order of parameters in $newTime = date($a['date'], strtotime('+3 hour'));. Try this:
<?php
$a['date'] = mktime();
$a11 = date("F j, Y, g:i a", $a['date']);
$newTime = date(strtotime('+3 hour'),$a['date']);
$b11 = date("F j, Y, g:i a", $newTime);
echo $a11 . " AND " . $b11;
?>
Dig it, you are not strtotime'ing the $newTime when converting to date, so it's false.
<?php
$a['date'] = time();
$a11 = date("F j, Y, g:i a", $a['date']);
echo 'Now = ' . time() . PHP_EOL;
echo 'Now +3hrs = ' . strtotime( '+3 hours' ) . PHP_EOL . PHP_EOL;
$newTime = strtotime( '+3 hours' );
$b11 = date("F j, Y, g:i a", $newTime );
echo $a11 . ' and ' . $b11 . PHP_EOL;
The format of date function is: string date ( string $format [, int $timestamp ] ). So, according to the first line, $a['date'] stores the timestamp value. But, according to the second line, its value is date format.
Moreover, you should type "+3 hours".
I add date like following
<?php
$a['date']="March 22, 2011, 10:22 pm";
$a11 = date("F j, Y, g:i a", strtotime($a['date']));
$b11 = strtotime(date("F j, Y, g:i a", strtotime($a['date'])) . " +3 hours");
$b11 = date("F j, Y, g:i a", $b11);
echo $a11 . "AND " . $b11;
?>

Categories