Get timestamp from date('z') and year - php

I know the year and the day of year (0-365).
Is there any way to get back from it to timestamp?
Something like:
$day=date('z');
$year=2012;
$timestamp=strtotime($day.'-nd day of '.$year);

Try the following:
$timestamp = mktime(0,0,0,1,$day,$year);
It will produce a timestamp where the time is set to 0:00:00.
(I am not sure if it will behave correctly with respect to daylight savings though...)
Check the PHP manual for further details: PHP: mktime - Manual

Maybe strtotime gets handy here:
php -r 'echo #date("Y-m-d H:s", strtotime("january 2012 +200 day")).PHP_EOL;'
In your example $timestamp = strtotime("january $year +$day day");

Try using createFromFormat :
$date = DateTime::createFromFormat('z Y', $day . ' ' . $year);
echo $date->getTimestamp();

Related

Update a unix timestamp php (strtotime)

I want to update a unix timestamp and add x months.
This is a timestamp that i use 1456256866
strtotime("+1 month")
what i want to accieve is :
$time = '1456256866';
//update $time with x months something like
$time("+5 month");
Can someone put me in the right direction?
Much Thanks
You could do something like below. the function strtotime takes a second argument.
$time = 1456256866;
$time = strtotime('+5 month', $time);
For such operations You should use Datetime class, especially Add method:
$date = new DateTime('#1456256866');
$date->add(new DateInterval('P5M'));
echo $date->format('Y-m-d') . "\n";
Check more here: http://php.net/manual/pl/datetime.add.php

get strtotime of specific time in php

I want to get the timestamp of a day/time for eg
17/12/2014 8pm
Currently I am doing
$curtime = strtotime(date("Y-m-d H:i:s"));
which is giving me the current timestamp but I need to be of 8pm.
Any help is highly appreciated. Thanks in advance.
If you're trying to get a timestamp of today at 8pm, it's actually much more simple than using date since you can use relative times in a strtotime:
$curtime = strtotime('today 8pm');
If you test this with date:
echo date('Y-m-d H:i:s', $curtime); // Returns 2014-12-17 20:00:00
Here's a whole writeup on relative date formats that explains how you can construct proper relative dates.
The best way to do this is using date_create_from_format. Checking out format's parameters, you will end up with something like this:
$date = date_create_from_format('d/m/Y ga', '17/12/2014 8pm');
if (!empty($date)) {//returns false if can't create date
$timestamp = $date->getTimestamp();
//echo date('d/m/Y H:i:s', $timestamp);
}

Calculating future date using php strtotime() not give right answer

I want to get future date after specific date
for this i use strtotime() function but it not work for me
then I use following code
$d1='2012-11-08';
$d2=date($d1, strtotime('+3 days'));
echo $d2;
output is
2012-11-08
2012-11-08
not
2012-11-11
but output is not 2012-11-11
output is 2012-11-08
i can't solve this what is error i do and how i solve this?
$d1='2012-11-08';
$d2=new DateTime($d1);
$d2->modify('+3 day');
echo $d2->format('Y-m-d');
Try using date_add
$d1 = '2012-11-08';
$d2 = date_add($d1, date_interval_create_from_date_string('3 days'));
echo $d2;
http://www.php.net/manual/en/datetime.add.php
If you're not running PHP 5.3, this should work:
$d1 = '2012-11-08';
$d2 = strtotime('+3 days', strtotime($d1));
echo date('Y-m-d', $d2);
You are using the date() function incorrectly. As per the documentation, date() takes a format string followed by an optional timestamp. You're giving it a date string and another date.
You can do what you want like this, where strtotime is used to modify the date,
$date = "2012-11-08";
echo date("Y-m-d", strtotime($date. " + 3 days"));
But if you're running PHP 5.2+ you should probably use the DateTime class, as it's got much better date handling, and it's easier to see what's going on with it.
$datetime = new DateTime("2012-11-08");
$datetime->modify("+ 3 days");
echo $datetime->format("Y-m-d");
Actually you wrongly added the strtotime(), it works when you use current date, if you want to add with customize date, You can try this,
<?php
$d1='2012-11-08';
$d2 = strtotime ( '+3 day' , strtotime ( $d1 ) ) ;
$d3 = date ( 'Y-m-d' , $d2);
?>
if you want to add date from current date, use the following,
$d1 = Date('Y-m-d', strtotime("+3 days"));
Whilst strtotime() is a handy tool, it is prone to locale issues.
I would instead use PHP's mature DateTime class, eg
$dt = new DateTime($d1);
echo $dt->add(new DateInterval('P3D'))->format('Y-m-d');

PHP and or mysql: Make a date from some variables

Assume that i have two variables in php:
$year
$month
Then I want to make another variable:
$date
which:
$date=$year-$month-25
So, if I have 2012 in $year and 7 for $month, $date will be 2012-07-25.
Actually, I will compare it with some date in MySQL.
$year and $month are inputted by user.
anybody have a solution?
The solution either how to make $date or anything as long it can be comparred with a date in mysql.
Thanks before. ^^
You can make a unix timestamp through this:
$myDate = mktime(0, 0, 0, $month, 25, $year);
This is a pretty useful thing to have, as you can format it into all sorts of nice via:
echo date("Y-m-d", $myDate);
// Prints something like: 2012-07-25
or
echo date("l", $myDate);
// Prints something like: Monday
or
date('l jS \of F Y h:i:s A', $myDate);
// Prints something like: Monday 8th of August 2005 03:12:46 PM
You can do as follows
$complete_date = $year."-".$month."-25";
which gives you 2012-7-25
Please, read the "php manual" for concat your PHP string.
it's not
$date = $year-$month-25;
it is
$date = $year . '-' . $month . '- 25';
or
$date = $year . "-" . $month . "- 25";
but simple quote is more optimize for php string.
The solution either how to make $date or anything as long it can be
comparred with a date in mysql
The key here is use of strtotime to create and compare.
MySQL dates can be converted to integer through the use of strototime:
strtotime($mysql_date);
Then you can get time() and compare to two:
time()<>strtotime($mysql_date) // then the two dates are not equal.
You can use mktime function
$date = date('Y-m-d',mktime(0,0,0,$month,25,$year));
Well, I would use mktime to get the timestamp of the date ( http://php.net/manual/de/function.mktime.php ) and use the command unix_timestamp(date(yourfield)) in mysql to compare them.
(the date() withing unix_timestamp is only required when you save datetime values and not pure date values)
Since mysql dates are usually in this format Y-m-d by default, you can use $thedate = date('Y-m-d',mktime(0,0,0,$month,25,$year)); where $month and $year are based on the user input. Of course you have to make the user input it in the format you want by using select/lists.

How to get next Date with specific time in PHP

I want to get the date with specific day and time in PHP, like i want the date of next day and time of 9.30 am i.e "2011-06-02 09:30:00".
the code i was using get to do that,
<?php
$next_day_date = date("Y")."-".date("m")."-".(date("d")+1)." 09:30:00";
$new_trig_time_stamp = strtotime($next_day_date);
$trigger_date_time = date("Y-m-d H:i:s",$new_trig_time_stamp);
echo $trigger_date_time;
?>
the code above works fine but fails on 31 day, on 31st it returns "1970-01-01 05:30:00".
Is there any other way to do so.
When shifting dates by a fixed number, it's better to use mktime(), because it handles invalid dates well (e.g. it knows that January 32 is in fact February 1)
$trigger_date_time = date("Y-m-d H:i:s", mktime(9,30,0, date('n'), date('j')+1, date('Y'));
strtotime() is very useful here.
$trigger_date_time = date( "Y-m-d H:i:s", strtotime( "tomorrow 9:30" ) );
Calculate it via unix timestamp - much less annoyance
<?php
$trigger_date_time = date("Y-m-d 09:30:00",time() + 60*60*24);
echo $trigger_date_time;
?>
echo date('Y-m-d', strtotime('+1 day')) . ' 09:30:30';
Have a look at date_add.
In more detail, something like...
$myDate = new DateTime("Some time");
$myDate->add(new DateInterval("P1D"));
You can then use $myDate->format(…) to extract formatted representations.

Categories