I've tired to add 10 weekdays to now. Everything is OK, but it clears the time part. Do you know why?
$now = date("Y-m-d H:i:s");
echo $now.'<br>';
$mod = strtotime($now." +10 weekdays");
echo $mod.'<br>';
echo date("Y-m-d H:i:s",$mod).'<br>';
Output:
2011-05-23 14:34:02
1307311200
2011-06-06 00:00:00
My expected output were:
2011-06-06 14:34:02
Thanks.
Looks like a difference in interpretation.
You could do the following to enforce the time:
<?php
$date = date("Y-m-d");
$time = date("H:i:s");
echo $date.' '.$time.'<br>';
$mod= strtotime($date." +10 weekdays $time");
echo $mod.'<br>';
echo date("Y-m-d H:i:s",$mod).'<br>';
There are several examples on the PHP documentation which can help you add days while still preserving the times.
Related
I'm basically trying to add 24 hours to a date with php and display it but it keeps adding only 23 hours in stead of 24 hours.
<?php
$create_time = strtotime('2015-03-18 20:03:23');
$set_time = $create_time + 3600*24;
echo gmdate("Y-m-d H:i:s", $set_time);
?>
So the result that I'm getting out of this is:
2015-03-19 19:03:23
but it's this what should be coming out of this:
2015-03-19 20:03:23
I'm new at working with these time functions and I can't figure out why it keeps getting adding 23 hours. Obviously I can multiply it by 25 and get 24 hours but that doesn't make sense to me.
So my question is: what's the proper way to add 24 hours to a date?
I would do it like so:
date("Y-m-d H:i:s", strtotime("+1 day"));
strtotime() uses default time zone, gmdate() uses Greenwich Mean Time (GMT). Try using date() instead.
<?php
$create_time = strtotime('2015-03-18 20:03:23');
$set_time = $create_time + 3600*24;
echo date("Y-m-d H:i:s", $set_time);
?>
I want to calculate EXACT past 30 days time period in php from now (for example 30 aug 14 23:06) to 30 days back (for example 1 aug 14 23:06). I wrote this where current datetime goes in $d1 and past 30 days datetime goes in $d2 but somehow i am not getting correct results. Any idea?
$url=$row["url"];
$pageid=getPageID($url);
$date=date('y-m-d g:i');
$d1=strtotime($date);
$d2=date(strtotime('today - 30 days'));
Thanks
The problem is likely caused by the malformed date() call. The first argument passed to date() should be the format (as shown in the Docs) and the second should be an optional timestamp.
Try this:
$d2 = date('c', strtotime('-30 days'));
PHPFiddle
As a short aside, the whole snippet can be simplified as follows:
$url = $row["url"];
$pageid = getPageID($url);
$date = date('y-m-d g:i');
$d1 = time();
$d2 = date('y-m-d g:i', strtotime('-30 days'));
You can also use the DateTime class's sub() method together with an DateInterval:
$now = new DateTime();
$back = $now->sub(DateInterval::createFromDateString('30 days'));
echo $back->format('y-m-d g:i');
if you would like to get out put as 2014-08-01 then try the below code. thanks
$date = '2014-08-30 23:06';
$new_date = date('Y-m-d G:i', strtotime($date.' - 29 days'));
echo "30 days back is " . $new_date;
From your brief description and example given, I believe that you want the date to be 30 days back and time to be the same as of now. The below code will serve this purpose. Thanks.
<?php
$date=date('y-m-d g:i');
$time=date('g:i');
echo "Todays date:" . $date. "<br>";
$d2 = date('y-m-d', strtotime('-30 days'));
echo "30 days back:" . $d2 . ' ' .$time;
?>
Try:
echo date("Y-m-d h:i:s",strtotime('-30 days'));
For more detail click here
Very simple two lines of code
$date = new DateTime();
echo $date->modify('-30 day')->format('y-m-d g:i');
I know you said with PHP, however, I can't imagine not getting the records from a DB. If you want to do so from the DB,use:
$sql='SELECT * FROM myTable WHERE date > CURRENT_DATE - INTERVAL 30 DAY';
$pdo->query($sql);
Very simple one lines of code:
echo (new DateTime())->modify('-30 day')->format('y-m-d g:i');
In the example below, it makes no sense if the variable $date is not
used anywhere else!
$date = new DateTime();
echo $date->modify('-30 day')->format('y-m-d g:i');
Sample answer is
$dateBack30Days=date('Y-m-d g:i', strtotime('-30 days'));
$today = gmdate("M-j-Y-H-i");
$date = gmdate('M-j-Y-H-i', strtotime($today . ' + 3 hours'));
echo $today ."<br/>";
echo $date;
and its result
Feb-24-2014-11-29
Feb-24-2014-14-00
it will add 3 hours but it also set Minutes is zero i want to add only 3 hours in current GMT time
This is happening because you are using $today variable to make + 3 hours, simply remove that and you are ready to go
$today = gmdate("M-j-Y-H-i");
$date = gmdate('M-j-Y-H-i', strtotime('+ 3 hours'));
echo $today ."\n";
echo $date;
//output Feb-24-2014-12-18
// Feb-24-2014-15-18
Live Sample Here
Those who are interested in a simpler version
$gmdate = gmdate(DATE_ATOM);
$loc_time = gmdate(DATE_ATOM, strtotime('+ 3 hours'));
DATA_ATOM and other formats which is basically same format used by MySQL 2018-10-28 13:14:18
I had gone through various stackoverflow solutions and other blogs but still it doesn't fix my problem.
Let's say that the date today is: 2013-12-28 and I want to get the date after 1 month and it is supposed to display 2014-01-28.
$date = date('o-m-d');
$final = date('o-m-d', strtotime("+1 month", $date));
echo $final;
Above is my code. It returns 02/01/1970.
I have also tried the mktime method but still it displays the 1970 output.
What am I doing wrong?
BTW. I am working this on a hosted server.
Thanks ahead. :)
Use DateTime function modify
$date = new DateTime( 'o-m-d' );
echo $date->modify( '+1 month' )->format('o-m-d');
If you want the current date +1 month use:
$final = date('o-m-d', strtotime("+1 month"));
Or with a given date:
$date = date('o-m-d');
$final = date('o-m-d', strtotime($date . " +1 month"));
echo $final;
If you want to use the second parameter of strtotime it has to be a timestamp.
Go the OOP way..
<?php
$date = new DateTime('2013-12-28');
$date->add(new DateInterval('P1M'));
echo $date->format('Y-m-d'); //prints 2014-01-28
The following code worked before, but failed to get the next 4 days of today 2011-11-04
$draw_date = '2011-11-04';
$ts = strtotime($draw_date) + 86400*4;
$ddate = date('Y-m-d', $ts);
echo $ddate;
The code above print 2011-11-07, but what I expected is 2011-11-08. It works if I set draw_date = '2011-10-04' or '2011-12-04'. Very weird! Can anyone explain why?
Thanks in advance.
Don't forget there's a DST switchover on November 6th. That makes 4days-from-now actually be 86400*4 + 3600 for the extra hour.
You can use:
$ts = strtotime($draw_date) ;
strtotime('+4 day', $ts);