PHP: producing relative date/time from timestamps

This function gives you “1 hour ago” or “Tomorrow” like results between ‘now’ and ‘specific timestamp’. function time2str($ts) { if(!ctype_digit($ts)) $ts = strtotime($ts); $diff = time() – $ts; if($diff == 0) return ‘now’; elseif($diff > 0) { $day_diff = floor($diff / 86400); if($day_diff == 0) { if($diff < 60) return ‘just now’; if($diff < 120) … Read more

How to convert date to timestamp in PHP?

This method works on both Windows and Unix and is time-zone aware, which is probably what you want if you work with dates. If you don’t care about timezone, or want to use the time zone your server uses: $d = DateTime::createFromFormat(‘d-m-Y H:i:s’, ’22-09-2008 00:00:00′); if ($d === false) { die(“Incorrect date string”); } else … Read more

How to get the unix timestamp in C#

As of .NET 4.6, there is DateTimeOffset.ToUnixTimeSeconds. This is an instance method, so you are expected to call it on an instance of DateTimeOffset. You can also cast any instance of DateTime, though beware the timezone. To get the current timestamp: DateTimeOffset.Now.ToUnixTimeSeconds() To get the timestamp from a DateTime: DateTime foo = DateTime.Now; long unixTime … Read more

Convert Date/Time for given Timezone – java

For me, the simplest way to do that is: Calendar calendar = Calendar.getInstance(); calendar.setTime(new Date()); SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd hh:mm:ss”); //Here you say to java the initial timezone. This is the secret sdf.setTimeZone(TimeZone.getTimeZone(“UTC”)); //Will print in UTC System.out.println(sdf.format(calendar.getTime())); //Here you set to your timezone sdf.setTimeZone(TimeZone.getDefault()); //Will print on your default Timezone System.out.println(sdf.format(calendar.getTime()));

PHP How to find the time elapsed since a date time? [duplicate]

Most of the answers seem focused around converting the date from a string to time. It seems you’re mostly thinking about getting the date into the ‘5 days ago’ format, etc.. right? This is how I’d go about doing that: $time = strtotime(‘2010-04-28 17:25:43’); echo ‘event happened ‘.humanTiming($time).’ ago’; function humanTiming ($time) { $time = … Read more

Timezone conversion in php

You can use the datetime object or their function aliases for this: Example (abridged from PHP Manual) date_default_timezone_set(‘Europe/London’); $datetime = new DateTime(‘2008-08-03 12:35:23’); echo $datetime->format(‘Y-m-d H:i:s’) . “\n”; $la_time = new DateTimeZone(‘America/Los_Angeles’); $datetime->setTimezone($la_time); echo $datetime->format(‘Y-m-d H:i:s’); Edit regarding comments but i cannt use this method because i need to show date in different time zones … Read more