PHP - Convert A Date To Unix Timestamp?
sorry, I know this is simple, but stuck on it...
how would one go about converting 1/21/11 to a UNIX timestamp, something like 1293035229? and while we are at it, how to convert 1293035229 to 1/21/11 ? (i know the timestamp is wrong, just for example) thanks!!!!! Similar TutorialsI want to convert a unix time stamp to the date format (dd/mm/yy). How can i be possible ? I want to have the timestamps of week numbers, this is working perfectly from the year 2011 to 2012. It refuses to switch to the year 2013. Do anyone know what i'm doing wrong?: <?php $year='2012'; $weekno='01'; $date = strtotime($year.'W'.$weekno); for($i=0;$i<55;$i++){ print date('d-m-Y W',$date)."\n"; $date=(weekno($date,'1'))."\n"; $date=(int)$date; } function weekno($date,$step=''){ $year=date('Y',$date); $weekno=date('W',$date); $date = strtotime($year.'W'.$weekno); if($step>0){ $date = strtotime($year.'W'.$weekno. '+1 week'); } if($step<0){ $date = strtotime($year.'W'.$weekno. '-1 week'); } return($date); } ?> I have to two variables $cronlast and $cronnow in my php code. I need to have the time 7am of previous day in $cronlast variable and 7am today in $cronnow variable. I need the time in UNIX timestamp format. So everyday when I print the variable I should have: $cronlast=7am Yesterday(in Unix Timestamp) $cronnow=7am Today(in Unix Timestamp) I would appreciate any help. Ok Let's say I have a timestamp: 1327482941 I want to convert this to output only "60" im not looking for a whole time_ago function, simply just need to convert a timestamp down to seconds ONLY not having minutes/years/etc no need none of that just purely seconds starting from 1-60 if so how is it possible? I tried to just Code: [Select] $seconds = substr($e,1,4) . ""; $e is my timestamp, I did this because I could just grab the last 2 digits in the timestamp that would be what I want, but that's not possible because it goes 1-90 not 1-60 ^_^ help Here is a code I am trying to make it only spit out 1-60 in seconds Code: [Select] function timeAgo4($timestamp, $granularity=1){ $difference = $timestamp; if($difference < 0) return '0'; elseif($difference < 86400){ $periods = array(60,'' => 1); $output = ''; foreach($periods as $key => $value){ if($difference >= $value){ $time = round($difference / $value); $difference %= $value; $output .= ($output ? ' ' : '').$time.' '; $granularity--; } if($granularity == 0) break; } return ($output ? $output : '0').''; } else return date($format, $timestamp); } Can anyone please tell me how I convert DD-MM-YYY (text) to TIME (unix) thought I'd say that I have this line at the top of all my pages, just in case it means a different code. Code: [Select] date_default_timezone_set('Europe/London'); //### Set the default timezone I have a string in php: Quote Sat, 28 Apr 2012 05:09:45 GMT I want to convert it to timestamp Example: 1335594473 Hi I have members DOB stored in a mysql table field called birthday in the following format YYYY-MM-DD I can't seem to convert the time stamp into the members age I need to be able to feed $birthday which is the field in mysql table that is in the above format and then use php code to convert to members age Everything I try doesn't seem to work I'm hitting a SQL Server db and every table has a ts (timestamp) field that is a binary datatype - 8 bytes. Is there a way to "read" the binary and output it as a string? Hi guys, I'm putting together a small event system where I want the user to add his own date and time into a textfield (I'll probably make this a series of drop-downs/a date picker later). This is then stored as a timestamp - "0000-00-00 00:00:00" which displays fine until I try to echo it out as a UK date in this format - jS F Y, which just gives today's date but not the inputted date. Here's the code I have right now: Code: [Select] $result = mysql_query("SELECT * FROM stuff.events ORDER BY eventdate ASC"); echo "<br />"; echo mysql_result($result, $i, 'eventvenue'); echo ", "; $dt = new DateTime($eventdate); echo $dt->format("jS F Y"); In my mysql table eventdate is set up as follows: field - eventdate type - timestamp length/values - blank default - current_timestamp collation - blank attributes - on update CURRENT_TIMESTAMP null - blank auto_increment - blank Any help as to why this could be happening would be much appreciated, thanks. I have tried a large number of "solutions" to this but everytime I use them I see 0000-00-00 in my date field instead of the date even though I echoed and can see that the date looks correct. Here's where I'm at: I have a drop down for the month (1-12) and date fields (1-31) as well as a text input field for the year. Using the POST array, I have combined them into the xxxx-xx-xx format that I am using in my field as a date field in mysql. <code> $date_value =$_POST['year'].'-'.$_POST['month'].'-'.$_POST['day']; echo $date_value; </code> This outputs 2012-5-7 in my test echo but 0000-00-00 in the database. I have tried unsuccessfully to use in a numberof suggested versions of: strtotime() mktime Any help would be extremely appreciated. I am aware that I need to validate this data and insure that it is a valid date. That I'm okay with. I would like some help on getting it into the database. Hi, I have a table field that has the full timestamp (date and time), I want to select records from DB but after checking the date part only from the timestamp. Example: 2020-3-10 10:15:00 ---- i want to check if current date is equal to the date part which in this case it is the same. Thanks. Hi am trying to add date and timestamp of when a comment gets posted.Am not gr8t at php but still learning. If any one can help me on this would be reil gr8t. Sry for the long post. As for now i have this Code: [Select] $date = date("M j, y, g:i a"); and echo's out the date, time [/code]$date[/code] as in the current date and time which i dont want. Code: [Select] <?php $query = yasDB_select("SELECT * FROM newsblog"); if($query->num_rows == 0) { echo '<div id="newsblog_text">This news blog has no comments, become a member and be the first to add one!</div>'; } else { $query = yasDB_select("SELECT * FROM newsblog WHERE userid = '$id'"); while($row = $query->fetch_array(MYSQLI_ASSOC)) { $date = date("M j, y, g:i a"); $text = $row['comment']; $text = str_replace(':D','<img src="' . $siteurl . 'templates/' . $theme . '/styles/images/smileys/biggrin.gif" title="biggrin" alt="biggrin" />',$text); $text = str_replace(':?','<img src="' . $siteurl . 'templates/' . $theme . '/styles/images/smileys/confused.gif" title="confused" alt="confused" />',$text); $text = str_replace('8)','<img src="' . $siteurl . 'templates/' . $theme . '/styles/images/smileys/cool.gif" title="cool" alt="cool" />',$text); $text = str_replace(':cry:','<img src="' . $siteurl . 'templates/' . $theme . '/styles/images/smileys/cry.gif" title="cry" alt="cry" />',$text); $text = str_replace(':shock:','<img src="' . $siteurl . 'templates/' . $theme . '/styles/images/smileys/eek.gif" title="eek" alt="eek" />',$text); $text = str_replace(':evil:','<img src="' . $siteurl . 'templates/' . $theme . '/styles/images/smileys/evil.gif" title="evil" alt="evil" />',$text); $text = str_replace(':lol:','<img src="' . $siteurl . 'templates/' . $theme . '/styles/images/smileys/lol.gif" title="lol" alt="lol" />',$text); $text = str_replace(':x','<img src="' . $siteurl . 'templates/' . $theme . '/styles/images/smileys/mad.gif" title="mad" alt="mad" />',$text); $text = str_replace(':P','<img src="' . $siteurl . 'templates/' . $theme . '/styles/images/smileys/razz.gif" title="razz" alt="razz" />',$text); $text = str_replace(':oops:','<img src="' . $siteurl . 'templates/' . $theme . '/styles/images/smileys/redface.gif" title="redface" alt="redface" />',$text); $text = str_replace(':roll:','<img src="' . $siteurl . 'templates/' . $theme . '/styles/images/smileys/rolleyes.gif" title="rolleyes" alt="rolleyes" />',$text); $text = str_replace(':(','<img src="' . $siteurl . 'templates/' . $theme . '/styles/images/smileys/sad.gif" title="sad" alt="sad" />',$text); $text = str_replace(':)','<img src="' . $siteurl . 'templates/' . $theme . '/styles/images/smileys/smile.gif" title="smile" alt="smile" />',$text); $text = str_replace(':o','<img src="' . $siteurl . 'templates/' . $theme . '/styles/images/smileys/surprised.gif" title="surprised" alt="surprised" />',$text); $text = str_replace(':twisted:','<img src="' . $siteurl . 'templates/' . $theme . '/styles/images/smileys/twisted.gif" title="twisted" alt="twisted" />',$text); $text = str_replace(':wink:','<img src="' . $siteurl . 'templates/' . $theme . '/styles/images/smileys/wink.gif" title="wink" alt="wink" />',$text); echo '<div class="newsblog_box1"><div class="newsblog_name">' . $row['name'] . ' '.$date.'</div></div> <div class="newsblog_box2">' . $text . '</div>'; } }?> I have a little problem with creating a timestamp and display it again with date () Here is a small shript: <?php $day = 6; $month = 7; $year = 1985; $ts = mktime($month, $day, $year); echo date("d-m-Y",$ts); ?> In my world would result in 6-7-1985 but instead I get 06-11-2010 Why .. what am I doing wrong? POSTBIL.COM Hi, I am trying to convert a String date into numeric date using PHP function's, but haven't found such function. Had a look at date(), strtotime(), getdate(); e.g. Apr 1 2011 -> 04-01-2011 Could someone please shed some light on this? Regards, Abhishek I'm having a weird issue going on, I'm wondering if it has to do with my php.ini or server setup, anyway here is the code with the odly outcomes: Code: [Select] echo date('Y/m/d H:i:s', 2145000000); // 2037/12/21 01:20:00 echo date('Y/m/d H:i:s', 2149000000); // 1901/12/31 01:58:24 echo date('Y/m/d H:i:s', 2150000000); // 1902/01/11 15:45:04 It seems after year 2037 it goes down to 1900+... Why would this happen? I've tried on 2 godaddy servers and local host and all same result I have a simple mysql database holding information about an image upload including its 'name' and 'date uploaded' [current_timestamp()] to a folder 'image' I then display it into a table using the php ‘foreach’ facility. I want the date to be displayed in the ‘dd/mm/yyyy’ format. not the default yyyy/mm/dd. Here is the relevant code <?php $conn = mysqli_connect("localhost", "root", "", "dbname"); $results = mysqli_query($conn, "SELECT * FROM tablename"); $image = mysqli_fetch_all($results, MYSQLI_ASSOC); ?> <?php foreach ($image as $user): ?> <td><p>NAME</p><?php echo $user['name']; ?></td> <td><p>COMMENT</p><?php echo $user['comment']; ?></td> <td><p>DATE</p><?php echo $user['date']; ?></td> What do I need to do? THANKS- Warren Following on from my problem with another Google Chart which jay0316 kindly helped with, I have another issue which I am hoping that someone might be able to help me with.
I am trying to put together a site for my wife to manage her diabetes and insulin intake. We want to monitor her blood/sugar glucose over periods of time (but with notes that she includes that can help monitor reasons for outlying results such as illness), so I am trying to include a Google Annotation Chart to do this, drawing on blood/glucose readings stored in a Database.
Where I am struggling at the moment is converting timestamps into information that I can include in the chart.
What I currently have at the moment (which obviously isn't working) is:
<script type="text/javascript" src="https://www.google.com/jsapi?autoload={'modules':[{'name':'visualization','version':'1','packages':['annotationchart']}]}"></script> <script type='text/javascript'> google.load('visualization', '1', {'packages':['annotationchart']}); google.setOnLoadCallback(drawChart); function drawChart() { var data = new google.visualization.DataTable(); data.addColumn('datetime', 'Date'); data.addColumn('number', 'Readings'); data.addColumn('string', 'Notes'); data.addRows([ <?php$annotate=DB::getInstance()->query("SELECT * FROM tabbyhealth WHERE reading!=0"); OK So I've got a datepicker that sends a date in d/m/y format. My DB stores the data in Unix Timestamp Which I can convert the date to with strtotime however this does the exact date & time. All I want is the actual day. I've spent hours trying to convert this with just the day with mixed results... Thanks. Hi In sql we have dates like this 134238 In php it shows - 19:10:11 . we can make it by using date('d.m.y', $date); where $date=134238 ; my question is how can we turn 19:10:11 in to 134238 using PHP Hi all, I'm having a bit of trouble a script running on a site where it converts a date of birth in a database shown like this '30/04/1993' to an actual age, for instance 18 in this case. Only the script I'm using below shows this age as 17, not 18 as it should be. Code: [Select] <?php $birthday = $row_getdets['dob']; function birthday ($birthday){ list($day,$month,$year) = explode("/",$birthday); $day_diff = date("d") - $day; $month_diff = date("m") - $month; $year_diff = date("Y") - $year; if ($day_diff < 0 || $month_diff < 0) $year_diff--; return $year_diff; } ?> So i've tried to remedy this myself with the following: Code: [Select] <?php $birthday = $row_getdets['dob']; function birthday ($birthday){ list($year,$month,$day) = explode("/",$birthday); $year_diff = date("Y") - $year; $month_diff = date("m") - $month; $day_diff = date("d") - $day; if ($month_diff < 0) $year_diff--; else if (($month_diff==0) && ($day_diff < 0)) $year_diff--; return $year_diff; } ?> ..but I'm having a syntax error (unexpected T_LINE), most probably down to my novice ability, I bet I've missed something simple. I'm still learning guys and I'd really appreciate any help at all. |