php date is wrong? - php

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;
?>

Related

How to add GMT +530 to - M j, Y, g:i a format

How to add GMT +530 to "M j, Y, g:i a" format using php. I am trying to add the GMT +530 Like
M j, Y, g:i a +530
But not able to get the desired result.
Try this
<?php
$date = date("M j, Y, g:i a", strtotime('+5 hours +30 minutes'));
echo $date;
?>

php for this week and next

I can get the date of the Monday for the current week, but how do I get it to display "and (the date for the monday of next week)"?
For example, "March 6 and 13, 2017"
This is what I have so far:
<?php echo date('F j, Y',strtotime(date('o-\WW')));?>
You can use the far more readable:
echo date('F j, Y',strtotime('Monday this week'));
echo date('F j, Y',strtotime('Monday next week'));
An exact script:
$this_monday = date("F j", strtotime("Monday this week"));
$next_monday = date("j, Y", strtotime("Monday next week"));
echo $this_monday . " and " . $next_monday;
The problem will be crossing months. If you need to do that, you'll need more logic, perhaps something like:
$this_monday = strtotime("Monday this week");
$next_monday = strtotime("Monday next week");
if (date("F",$this_monday) === date("F",$next_monday)) {
echo date("F j", $this_monday) . " and " . date("j, Y", $next_monday);
} else {
echo date("F j", $this_monday) . " and " . date("F j, Y", $next_monday);
}
Someone may come along and suggest using the Datetime class instead, and that might be something to put on your list to learn, as well.

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 :)

Check Hours in a date format

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.
}

Altering numbers/date and adding + one week. Not with date() - in PHP

I have:
PHP code: $date = date("F j, Y, g:i a"); and send this to my datebase
I use
$date = $gg['date'];
to get the date from my datebase
When I echo $date -> June 30, 2012, 3:45 pm
The time is already set in the database with mail.php, and in ordertracking.php I'm getting that time and want to add one week to it.
So I want to add one week to: $date = $gg['date'];
$connection = mysql_connect("localhost","root","") or die ("Can't connect");
mysql_select_db("shoppingcart", $connection) or die ("Can't connect");
$ordertracking = mysql_query("SELECT * FROM `ordertracking` WHERE orderid='$orderid'");
while($gg=mysql_fetch_array($ordertracking))
{
$progress = $gg['progress'];
$date = $gg['date'];
}
mysql_close($connection)
Use the DateTime modify() method
$dt = DateTime::createFromFormat("F j, Y, g:i a", $date);
$dt->modify('+1 week');
echo $dt->format("F j, Y, g:i a");
Here some simple solution)
$date = date( 'F j, Y, g:i a' );
echo date( 'F j, Y, g:i a', strtotime( $date ) + 7 * 24 * 60 * 60 );
Can you try this:
$date = 'June 30, 2012, 3:45 pm';
$new_date = date("F j, Y, g:i a",strtotime(date("F j, Y, g:i a", strtotime($date)) . " +1 week"));
Hope this helps.
If you wnat to use the strtotime method you would do
$date = date('F j, Y, g:i a', strtotime('2012-06-30 3:45 pm +1 week'));

Categories