convert date to unixtime php - php

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

Related

convert a date into a timestamp [duplicate]

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

Random Pick a Month From Last 3 Months PHP

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;

Get a dynamic timestamp in php?

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);

How to set an specific date?

I'm trying to create an date with
$date_end = mktime(0, 0, 0, date('m'), date('d')+7, date('Y'), $date_set);
The output is today + 7 days instead of the date given + 7.
The manual says nothing about mktime() taking a date as argument.
Use strtotime("+7 days", $date_set).
$date_end = mktime(0, 0, 0, date('m', $date_set), date('d', $date_set)+7, date('Y', $date_set));
is, I believe, what you were trying to accomplish (assuming $date_set is a timestamp). Else, #Kristian's suggestion I believe is a good one.
Why are you passing a $date_set variable, and why are you using mktime if you already have the time?
Simply add 7 days: $date_end = $date_set + (7 * 86400);

Beginner's question on what to add in a PHP time variable

I found this script on php.net and finds the difference between now and a future day. My question is very simple, but is a sample time or how can I make that time that it is needed for the $future_date ? Also what is the purpose of -1970 ?
Also how can I show a message when the future_date is reached or passed?
<?php
function time_difference($endtime){
$days= (date("j",$endtime)-1);
$months =(date("n",$endtime)-1);
$years =(date("Y",$endtime)-1970);
$hours =date("G",$endtime);
$mins =date("i",$endtime);
$secs =date("s",$endtime);
$diff="'day': ".$days.",'month': ".$months.",'year': ".$years.",'hour': ".$hours.",'min': ".$mins.",'sec': ".$secs;
return $diff;
}
$end_time = $future_date - time();
$difference = time_difference($end_time);
echo $difference;
//sample output
'day': 2,'month': 1,'year': 0,'hour': 2,'min': 05,'sec': 41
?>
A unix timestamp checkout the docs for time() and mktime()
You're substracting two values from each other so they need to be compatibable formats to be able to do that. Checking the documentation on time() could have saved you from this question.
date() is also a function you might want to check up on. Using date and the right parameters it will return the current year(Y) month(m) or day of the month(d) you can add and substract to these values and then pass them into mktime to get a unix timestamp like so for the current year in unix timestamp format:
$currentyear = mktime(date(Y));
Below would set $future_date to 1st Dec 2011
$future_date = mktime(0, 0, 0, 12, 1, 2011);
So Hour Min Sec goes:
$future_date = mktime(H, M, S, 12, 1, 2011);
Below would be 13:21:59 1st Dec 2011
$future_date = mktime(13, 21, 59, 12, 1, 2011);
$future_date should be a unix timestamp.
$future_date = strtotime("next week");
To check if the time has been reached
if($future_date <= time()) echo "Date reached";
$future_date would be an integer timestamp (in seconds since Jan 1, 1970) representing some time in the future.
ie:
$nextWeek = time() + (7 * 24 * 60 * 60);
Takes the current date/time and adds 7 days worth of seconds (24 hours, 60 minutes per hour, 60 seconds per minute) to get the integer time of one week from now.
Jan 1, 1970 is significant - it is called the Epoch in UNIX (January 1 1970 00:00:00 GMT) and is often used as a starting point for dates and/or computer "time" (time zero).

Categories