How would you calculate an unix timestamp of the January 1st of the current year in PHP?
What I mean by current year is that I don't want to put 2012 in it because it should be a dynamic date, next year it should be 2013 and so on.
You can use :
mktime(0, 0, 0, 1, 1, date('Y'));
You can do this:
echo strtotime('01 january ' . date('Y'));
You mean you have a date and time like this 2012-01-01 06:30:54 and you want to convert them to unix timestamp?
If that's the case then strtotime() is the best way to go buddy.
e.g : strtotime("2012-01-01 06:30:54");
or
$datetime = "2012-01-01 06:30:54";
echo strtotime($datetime);
Related
I have a date string that contains the month and the day: "January 20"
I need to convert this to a timestamp in PHP and can use strtotime to do so. The catch is I always want to return a future timestamp. So if it's December 30, 2020 today, and I have the string "January 20," I want to return the timestamp for January 20, 2021 -- not January 20, 2020.
I've come up with a few convoluted ways of doing this, such as converting to a timestamp then making sure the timestamp is > the current time. If it's not, add a year and re-convert to a timestamp. But it seems like there may be a best practice and simple way of doing this. Any suggestions?
Thanks,
Tim
I'd try to compare current timestamp against this year timestamp. It should work.
$str = 'January 20';
$now = time();
$thisYear = strtotime($str);
$nextYear = strtotime($str . ' + 1 year');
$futureDate = $thisYear < $now ? $nextYear : $thisYear;
// 2021-01-20 - for January 20
// 2022-01-01 - for January 01
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
Okay so basically I have two variables one called $day and another called $time what I'm trying to do is convert both of these into a unix time-stamp so for example;
$day = 'Monday';
$time = '14:00:00';
So what I'm looking for is a $timestamp variable that would echo out the next Monday coming up at 14:00:00 in a unix timestamp format.
I'm guessing the hardest part of this would be the fact that the day is not a specific date more a day of the week meaning it would have to select the next monday coming up, or the next tuesday... for example.
Thanks for any help.
The constructor for the DateTime class is pretty good at figuring this sort of thing out:
<?php
$day = 'Monday';
$time = '14:00:00';
$date = new DateTime("next $day $time");
echo $date->getTimestamp();
// 1475503200
$datetime = new DateTime();
echo $datetime->format('U');
Solution One:
mktime - Get Unix timestamp for a date
echo mktime(23, 24, 0, 11, 3, 2009);
1257290640
Returns the Unix timestamp corresponding to the arguments given. This timestamp is a long integer containing the number of seconds between the Unix Epoch (January 1 1970 00:00:00 GMT) and the time specified.
Arguments may be left out in order from right to left; any arguments thus omitted will be set to the current value according to the local date and time.
mktime($isAM ? $hrs : ($hrs + 12), $mins, $secs, $m, $d, $y);
To handle AM/PM just add 12 to hours if PM.
Solution Two:
strtotime Returns a timestamp on success, FALSE otherwise.
echo strtotime('2012-07-25 14:35:08' );
Output:
1343219708
I want to random pick a month from this month and last 3 months. Now is February'16, so the last 3 months January'16, December'15 and November'15.
Below is the php I used:
$month = mt_rand(date("m",strtotime("-3 Months")),date('m'));
However I got this error:
mt_rand(): max(2) is smaller than min(11)
because 2nd param is smaller than the 1st param. How to fix this?
I would use the unix timestamps then use the date to format the timestamp.
echo date("m", mt_rand(strtotime("-3 Months"), time()));
Format the date as you choose, http://php.net/manual/en/function.date.php.
am i to verbose with this:
$new_date =date('F Y', mktime(0, 0, 0, date("m")-rand(0,3) , date("d"), date("Y")));
echo $new_date;
I'm trying to get the number of month before of the current month (now is 04 (april), so I'm trying to get 03). I'm trying this:
date('m')-1;
but I get 3. But what I want is to get 03.
The correct way to do this really is:
date('m', strtotime('-1 month'));
As you will see strange things happen in January with other answers.
The currently accepted response will result in an incorrect answer whenever the day of the month (for the current day) is a larger number than the last day of the month for the previous month.
e.g. The result of executing date('m', strtotime('-1 month')); on March 29th (in a non-leap-year) will be 03, because 29 is larger than any day of the month for February, and thus strtotime('-1 month') will actually return March 1st.
Instead, use the following:
date('n') - 1;
You may be surprised, but date() function manual page has an exact example of what you need:
$lastmonth = mktime(0, 0, 0, date("m")-1, date("d"), date("Y"));
The result of your calculation is a number. If you want to format it like a string, you can use:
$result = date('m')-1;
$string_result = sprintf("%02s", $result);
Edit: Note that this is only a partial solution to format a number like a string.
intval(date('m'))
for the current month
(intval(date('m'))-1)%12
for the previous month, also for december/january
date('m', strtotime('last month'));
This will work regardless of whether or not you're in January
This works too.
printf("%02s", (date('m') - 1));
This should do it for you...
str_pad(date('m')-1, 2, '0', STR_PAD_LEFT);
I have a form which posts date information month, day, yeah, hour, minute, am/pm. How do i encode/decode this to and from unixtime using php?
mktime() - Get Unix timestamp for a date
echo mktime(23, 24, 0, 11, 3, 2009);
1257290640
To handle AM/PM just add 12 to hours if PM.
mktime($isAM ? $hrs : ($hrs + 12), $mins, $secs, $m, $d, $y);
Alternatively you could use strtotime():
strtotime() - Parse about any English textual datetime description into a Unix timestamp
echo strtotime("2009-11-03 11:24:00PM");
1257290640
Use the mktime function