Ok so im trying to display a date that i got from a database
Here's what i can display without any formatting: 2012-11-21 19:00:00
what im trying to display is: 2012-11-21 at 19:00:00
below is the code
$expiryDate = date('Y/m/d', $data[0]->dinExpiry);
$expiryTime = date('H:i', $data[0]->dinExpiry);
And this is the error im getting;
Also the date that it displays is wrong
Thanks in advance for the help
Easy solution
You say that you can display 2012-11-21 19:00:00 without any formatting.
So if you can display an arbitrary $date as 2012-11-21 19:00:00, why not just substring it:
$dateToDisplay = substr($date, 0, 11).'at '.substr($date, 11, 8);
and show $dateToDisplay as 2012-11-21 at 19:00:00.
A little more...
It's tricky to answer this without knowing the output of $data[0]->dinExpiry. (I'll assume it gives you a variable called $date).
As #Marc B says, the fact that you're getting 1970 out of it shows that it's not a valid timestamp (hint: it's a string), so it's evaluated to zero seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).
You can convert it to a valid timestamp like so:
$expiryDate = date('Y-m-d', strtotime($date));
Or to match your example:
$expiryDate = date('Y-m-d', strtotime($data[0]->dinExpiry));
$expiryTime = date('H:i', strtotime($data[0]->dinExpiry));
Related
is it possible to add a variable string like '2 day, 2 weeks or even 4 hours' to a date time in PHP.
For example:
I have a date time like this: '2017-08-02 12:00'
now the user choose an interval like '4 hours or 2 weeks'
now the user choice should be added to the date time.
Is this possible?
I don't want the whole code, maybe just an advice how to do that.
thanks
Yes, use
$userDate = strtotime('2017-08-02 12:00:00');
echo date('Y-m-d H:i:s', strtotime('+4 hours', $userDate));
to get date after 4 hours
Example
Explanation
strtotime converts about any English textual datetime description into a Unix timestamp. Most commonly it's used with actual date string or with difference string. E.g. +5 months, 'next Monday' and so on. It will return Unix timestamp - integer that represents how much seconds there is after 1970-01-01 (1970-01-01 00:00:00 is 0, 1970-01-01 00:01:00 is 60 and so on).
So in strtotime('2017-08-02 12:00:00') we convert date to integer for later use.
strtotime('+4 hours', $userDate) - here we use our date as "now" parameter (by default it's time()) and requesting to return timestamp after 4 hours.
echo date('Y-m-d H:i:s', ...); - date accepts format and Unix timestamp to convert from integer back to human readable text.
May be you are looking for this:
http://php.net/manual/en/datetime.modify.php
$date = new DateTime('2006-12-12');
$date->modify('+1 day');
echo $date->format('Y-m-d');
For a datetime you can use the add method but you have to put in the correct code for the amount to add.
$my_date = new DateTime();
$addition = 4;
$my_new_date = $my_date->add(new DateInterval("P${addition}D"));
echo $my_new_date->format('Y-m-d H:i:s');
Where addition is your variable that you want to add.
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 use a PHP script for a banlist that fetches the date and time, but I have an error and don't know how to convert it to "normal" time.
It lists me only the date : 01.01.1970 um 00:00 Uhr
The script part from this:
//Convert Epoch Time to Standard format
$datetime = date("d.m.Y \\u\\m H:i \\U\\h\\r", $row['expires']);
echo "<td>$datetime</td>";
This is the entry from the mysql db: http://fs1.directupload.net/images/141208/9ugjslm8.jpg
Dont know if it helps?
Have anyone an idea to solve this?
LINK: http://pastebin.com/r0dXg8FX
The row comes from an plugin. for example: $row['name'] , $row ['banner'], $row['reason'] this comes all from the plugin
It lists me only the date : 01.01.1970 um 00:00 Uhr
That simply means you are thinking of $row['expires'] incorrectly. That is not a UNIX Timestamp value and is producing an invalid date. It means the value essentially evaluates to 0, which is Jan 1st 1970 in UNIX time
date() requires you to send a valid Unix timestamp to it (INT 11), is that what you have in database for that field? or it is a date time field?
Try this
echo date("d.m.Y \\u\\m H:i \\U\\h\\r", "2014-10-12"); //invalid
echo date("d.m.Y \\u\\m H:i \\U\\h\\r", time()); //valid: current unix timestamp
Based on this image (that you have given in deleted answer) you have milliseconds. Divide milliseconds with 1000 to get seconds.
Probably something like this:
$datetime = date('d.m.Y \u\m H:i \U\h\r', $row['expires'] / 1000);
But, if you get year 1970, that means that you are on 32bit machine, and your fetched INT is out of range. In this case you could just cut last 3 numbers with string function substr():
$datetime = date('d.m.Y \u\m H:i \U\h\r', substr($row['expires'], 0, -3));
Convert the date to UNIX timestamp first. Try with -
$datetime = date("d.m.Y \\u\\m H:i \\U\\h\\r", strtotime($row['expires']));
echo "<td>$datetime</td>";
I don't know why it is so confusing, maybe it s because there are so many ways to output/input date and time with mysql and php. All I want is for users to put a date in this format
MM/DD/YYYY
and for it to output in this format
Month Name - Date
and for time, I want users to pick the time from a select field, starting with 1:00 P.M. and incrementing down 30 minutes, so it will be 1:30 P.M., 2:00 P.M... and I want to somehow insert this into MySQL and output it as 1:30 P.M. Not sure how to do that either.
The best way to store a date format in a database and be able to display it the way you want is to use the time() and date() function.
when storing the date in your database you should use time(), it will generate a string like this -> 1300695900. this little string contains the date and time
then you can use date to display it in any way you want:
$time = time();//you would normally get this from the database
date('d M Y', $time); //outputs 21 Mar 2011
date('m-d-y', $time); // outpus 03-21-2011
and so on...
Edit:
to answer your last question, you just get the different values, stick it together (concatenation) and then use strtotime()
$date = $_POST['date']; // eg 03/03/2011
$time = $_POST['time']; // eg 1:30
$daypart = $_POST['daypart']; // eg PM
$date_time = $date.' '.$time.' '.$daypart;
$strtime = strtotime($date_time);
echo date('d M Y - h:i', $strtime);// outputs 03 Mar 2011 - 01:30
can anyone get me a code for hiding the time stamp from a string.
I used this code to get the date from the string
suppose the
$date_string = "02/06/2011 11:00 am - 2:00 pm";
$date = strtotime($date_string);
$date = date('m/d/y', $date);
But the out put I am getting is something like this
1/1/70
Please suggest a better way that I could implement for this to work
I want it to show like
02/06/2011
If the date you're looking for is already in the string and all you want to do is remove the time range, you don't need any date manipulation. Just remove everything after the space (assuming the format of the date_string remains consistent).
$date_string = "02/06/2011 11:00 am - 2:00 pm";
$date = explode(" ",$date_string);
echo $date[0];
Or even simpler (but untested)
echo strtok($date_string," "); //http://codepad.org/Or1mpYOp
PHP.NET:strtok
PHP.NET:explode
$date = strtotime($date_string);
$date = getdate($date);
$date = $date['mon'] . '/' . $date['mday'] . '/' . $date['year']
If 02/06/2011 11:00 am - 2:00 pm is what gets displayes, you're obviously displaying $date_string and not $date, because strtotime('02/06/2011 11:00 am - 2:00 pm'); returns boolean false, which date('m/d/y', $date) would convert to 01/01/1970.
Try something like this
$date_string = "02/06/2011 11:00 am - 2:00 pm";
$date_exploded = explode('-',$date_string);
$date = strtotime($date_exploded[0]);
$date = date('m/d/y', $date);
echo $date;
Leaving aside the fact that something is going very wrong in the date parsing, you need to be aware that using a date format of 00/00/00[00] is rather ambigious - in the US dates written like this are in the format mm/dd/yy[yy] while in the UK it is interpreted as dd/mm/yy[yy]. The strtotime function does not use the locale setting to work out which applies and always assumes the former.
If I were being asked to parse this, I'd go with using preg to extract the date part. Using a pattern:
/([0-9]{1,2})\/([0-9]{1,2})\/([0-9]{2,4})/
gives an array
0=> 02/06/2011
1=> 02
2=> 06
3=> 2011
Then use mktime to generate the unix timstamp.
Other approaches include using substr to extract a fixed length string, or exploide by space to get words.
If your string is already is date .
$date = substr($records[$datetime, 0, 10);