php strtotime one week ago wrong? - php

I need to get the milliseconds of exactly on week ago.
I tried this:
$this->weekDate = strtotime("-1 week");
echo($this->weekDate);
it returns:
1422536434
which according to this conversion tool:
http://www.fileformat.info/tip/java/date2millis.htm
is
Samstag, 17. Januar 1970 11:08 Uhr GMT
Anybody an idea what is wrong?

The above code is printing the time in seconds Use the code below
$this->weekDate = strtotime("-1 week");
echo($this->weekDate * 1000);
The output of the above code will be
1,422,536,434,000
Hope this helps you

It might be because of the timezones (you didn't mention when you were trying to calculate the 1 week ago so it's hard to tell what output you expected).
Try this:
//get current time
$now = time();
//output in human readable format
echo 'now: ' . date('r', $now) . '<br />';
//calculate same time 1 week ago
$this->weekDate = strtotime("-1 week", $now);
//output it to compare
echo '1 week ago: ' . date('r', weekAgo));

Related

PHP Get 52 week start/end date for a specific year

I would like to calculate the start date from Tuesday and the end date to land on Mondays in the year 2016
I am trying to have the output
start date("j/n/Y"); end date("j/n/Y");
I have looked at similar SO questions, and google searches but its for specific week #'s, or not what I am trying to accomplish.
What I am trying to get is the following.
Start: 12/14/2015
End 12/21/2015
Start: 12/22/2015
End 12/28/2015
....
Start: 01/05/2016
End 01/11/2016
Start: 01/12/2016
End 01/18/2016
...
so on and soforth.
Any help would be greatly appreciated, and hope people dont downvote the crap out of this question. it is valid, and I have looked and cannot seem to find what I am after.
You can use the strtotime function to have the first tuesday of january 2016 and then loop 52 times, using next monday, and tomorrow. So you don't have to do any calculation.
<?php
$ts=strtotime("tuesday january 2016");
echo "<table>";
for ($i=0;$i<52;$i++) {
echo "<tr><td>Start: ".date("Y/m/d", $ts);
$ts=strtotime("next monday", $ts);
echo "</td><td>End: ".date("Y/m/d", $ts)."</td>\n";
echo "</td><td>Payweek: ".date("W", $ts)."</td></tr>\n";
$ts=strtotime("tomorrow", $ts);
}
echo "</table>";
You can try following code:
$start_date=date("m-d-Y", strtotime('tuesday this week'));
$end_date=date("m-d-Y", strtotime('monday next week'));
It will give you week starting from Tuesday and ending from Monday.
$oneWeek = 7*24*60*60; // 1 week in seconds
$curr_ts = time();
while ($curr_ts < strtotime('2017-01-01')) {
echo " Start: ",
date('m/d/Y', strtotime('tuesday this week', $curr_ts)),
"\nEnd ",
date('m/d/Y', strtotime('monday next week', $curr_ts)),
"\n";
$curr_ts = $curr_ts + $oneWeek;
}
While the date is less than your defined end date, loop over each week.
$date = Some start date ...;
$endDate = Some end date ...;
while (date_interval_format($date->diff($endDate),'%R') == '+') {
echo $date->format('j/n/Y') . " - " . $date->add(new DateInterval('P6D'))->format('j/n/Y') . "\n";
$date->add(new DateInterval('P1D'));
}

PHP get next occurrence of Monday from a certain date (with time)

I'm looking for the next Thursday after a specific date, say 2014-02-25. The problem I'm having here is that when I use the below code, the time seems to be erased.
<?php
$timestamp = '2014-02-25 10:30:00';
echo date('Y-m-d H:i:s', strtotime("next Thursday", strtotime($timestamp)));
?>
The result I am getting is 2014-02-27 00:00:00 when I would like it to be 2014-02-27 10:30:00
Is there something I am missing here that is causing the time to be set to midnight?
I appreciate the help, thanks.
There is no time format that can directly express this. You need to produce a format like
next Thursday 10:30:00
... manually and pass that to strtotime(). The time information you need to extract from the reference time string. Like this:
$refdate = '2014-02-25 10:30:00';
$timestamp = strtotime($refdate);
echo date('Y-m-d H:i:s',
strtotime("next Thursday " . date('H:i:s', $timestamp), $timestamp)
);
The same results could be achieved using string concatenation:
echo date('Y-m-d', strtotime("next Thursday", $timestamp)
. ' ' . date('H:i:s', $timestamp);
The documentation for so called relative time formats can be found here

PHP mktime() is returning wrong date

im doing some stuff with mktime, i need to add the next date with 30 days more but its returning me 1970-01-30 date, what im doing wrong ?
$strtime=strtotime("2013-10-04");
$fecha=date("Y-m-d",$strtime);
echo $fecha."<br />";
$nueva_fecha=mktime(0,0,0,date("n",$fecha),date("j",$fecha)+30,date("Y",$fecha));
echo date("Y-m-d",$nueva_fecha)."<br />";
Result:
2013-10-04
1970-01-30
Date is looking for a timestamp as it's 2nd parameter, not a string value representing this. Updated to pass it $strtime instead.
$strtime=strtotime("2013-10-04");
$fecha=date("Y-m-d",$strtime); // <-- Unnecessary unless you want to echo the value.
echo $fecha."<br />";
$nueva_fecha=mktime(0,0,0,date("n",$strtime),date("j",$strtime)+30,date("Y",$strtime));
echo date("Y-m-d",$nueva_fecha)."<br />";
Output:
2013-10-04
2013-11-03
You can just use the following function to add 30 days to the date you put in:
$date = strtotime("2013-10-04");
$new_date = strtotime("+30 days", $date);
or simply to the current date:
$new_date = strototime("+30 days", time());
If you already have strtotime, why plus on date ? Instead you could've used + (30 days in seconds) OR simply the feature they offer you + 1 day check answer: adding one day to a date
strtotime('2013-10-04 + 30 days');
This will print 2013-11-03:
date('Y-m-d', strtotime('2013-10-04 + 30 days'))
you can try this:
echo strtotime("+1 day"), "\n";
echo strtotime("+30 day",strtotime(date('D, d M Y'))), "\n";
this will add 30 days to the current date.
Also strtotime is very usefull you can use it for weekly,monthly and yearly.
You can use this also
<?php
$date = date("Y/m/d"); // example date in yyyy/mm/dd format
$unix_time = strtotime( $date ); // covert date to unix time
$sec_in_30_days = 60 * 60 * 24 * 30; // 60 seconds * 60 minutes * 24 hours * 30 days
$new_unix_time = $unix_time + $sec_in_30_days; // add 30 days to unix time
$date_in_30_days = date( 'Y/m/d', $new_unix_time ); // convert new unix time to date
// Output results:
echo 'original current date: ' . $date . '<br />';
echo '<br />';
echo 'new date: ' . $date_in_30_days . '<br />';
?>
Output will be
original current date: 2013/10/04
new date: 2013/11/03

One day added in gmdate

I'm trying to understand how gmdate works, i have the next simple code:
<?
$seconds = 86399;
echo gmdate("d \d\a\y\s H:i:s",$seconds);
?>
The result i was expecting is 0 days 23:59:59, but i get 1 days 23:59:59 why is returning one day?
I know that i can do something like this to avoid the problem:
<?
define("SECONDS_BY_DAY",86400);
$seconds = 86399;
echo floor($seconds / SECONDS_BY_DAY) . " days ";
echo gmdate("H:i:s",$seconds);
?>
But i want to understand why gmdate is returning one day instead of 0
The d format specifier does not print the number of whole days passed since the start of the Unix epoch; it prints the day of month.
Your $seconds value corresponds to 23:59:59 on January 1, 1970 -- hence d is 1.
To get the total number of days since the epoch start, use
$date = new DateTime('#'.$seconds);
$epoch = new DateTime("#0");
$diff = $date->diff($epoch);
echo $diff->days;

How do I add 24 hours to a unix timestamp in php?

I would like to add 24 hours to the timestamp for now. How do I find the unix timestamp number for 24 hours so I can add it to the timestamp for right now?
I also would like to know how to add 48 hours or multiple days to the current timestamp.
How can I go best about doing this?
You probably want to add one day rather than 24 hours. Not all days have 24 hours due to (among other circumstances) daylight saving time:
strtotime('+1 day', $timestamp);
A Unix timestamp is simply the number of seconds since January the first 1970, so to add 24 hours to a Unix timestamp we just add the number of seconds in 24 hours. (24 * 60 *60)
time() + 24*60*60;
Add 24*3600 which is the number of seconds in 24Hours
Unix timestamp is in seconds, so simply add the corresponding number of seconds to the timestamp:
$timeInFuture = time() + (60 * 60 * 24);
You could use the DateTime class as well:
$timestamp = mktime(15, 30, 00, 3, 28, 2015);
$d = new DateTime();
$d->setTimestamp($timestamp);
Add a Period of 1 Day:
$d->add(new DateInterval('P1D'));
echo $d->format('c');
See DateInterval for more details.
As you have said if you want to add 24 hours to the timestamp for right now then simply you can do:
<?php echo strtotime('+1 day'); ?>
Above code will add 1 day or 24 hours to your current timestamp.
in place of +1 day you can take whatever you want, As php manual says strtotime can Parse about any English textual datetime description into a Unix timestamp.
examples from the manual are as below:
<?php
echo strtotime("now"), "\n";
echo strtotime("10 September 2000"), "\n";
echo strtotime("+1 day"), "\n";
echo strtotime("+1 week"), "\n";
echo strtotime("+1 week 2 days 4 hours 2 seconds"), "\n";
echo strtotime("next Thursday"), "\n";
echo strtotime("last Monday"), "\n";
?>
$time = date("H:i", strtotime($today . " +5 hours +30 minutes"));
//+5 hours +30 minutes Time Zone +5:30 (Asia/Kolkata)

Categories