Altering numbers/date and adding + one week. Not with date() - in PHP - 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'));

Related

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

Date Format is not working

i have a string that is like this 2012/10/12 10:03:46 (Year/Month/Day) format now i want to change its format to something like this October 12, 2012 10:03 p.m i have tried php's DateTime class but its not working:
<?php
$date = new DateTime();
$date->createFromFormat('Y/m/dd H:i:s', substr($suggestion->suggestion->created_at, 0,19));
echo $date->format('d-m, y h:i A');
?>
can anyone tell me whats wrong and how can i correct it??
You should have seen at least one notice something like "don't call static function in non-static context".
DateTime::createFomFormat()
$date = DateTime::createFromFormat('Y/m/d H:i:s', substr($suggestion->suggestion->created_at, 0,19));
echo $date->format('F j, Y, g:i a');
It's a static method and (as the name suggests) it creates a new instance of DateTime.
$today = date("F j, Y, g:i a");
<?php
$date = new DateTime('2012/10/12 10:03:46');
echo $date->format('d-M, y h:i:s A');
//output - 12-Oct, 12 10:03:46 AM
?>
Try this...
$today = date("F j, Y h:i a"); // gives October 15, 2012 5:20 PM
Try this...
$today = date("F j, Y g:i A"); // gives October 15, 2012 5:20 PM (g removes leading zeroes)
Your case
$date = new DateTime('2012/10/12 10:03:46');
echo $date->format('F j, Y h:i A'); // gives October 15, 2012 10:03 AM
Reference : click

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