Unix time to weeks - php

How can I easy convert a unix time to a weeks, days or months?
For example I got 604800 in unix time, which means thats 1 week, 3600 is 1 day etc.
I know this can be done using divisions, but I wonder if there is any ready-function for this.

From a timestamp, you can get much information using date(). For example:
$year = date('Y', $timestamp);
$dayOfYear = date('z', $timestamp);
$weekOfYear = date('W', $timestamp);

CakePHP have nice implementation of this, look at http://api13.cakephp.org/view_source/time-helper/#line-463

If you are trying to convert seconds to a week number and so on use date() function

Related

how to get week +4 in this day with carbon

I have a problem here I want to get the +4 week date from the current date using carbon, the +4 week plan will be dynamic depending on the input entered by the user, how do I make it, I've tried using this code but it's time to step back
$dt = Carbon::now();
dd($dt->week(4)->format('Y-m-d'));
Check Carbon docs, you may use addWeeks():
$dt = Carbon::now();
dd($dt->addWeeks(4)->format('Y-m-d'));
The week() method you've used, sets the week number using given first day of week and first day of year included in the first week.
I am not sure to understand your question, but I guess you just have to use :
$dt = Carbon::now();
$dt->addWeeks(4);
dd($dt->format('Y-m-d');
You don't need carbon for such simple tasks.
With DateTime
echo date_create('+4 weeks')->format("Y-m-d");
or with date and strtotime
echo date("Y-m-d",strtotime('+4 weeks'));

Find PHP microtime 1 day ago

How to find 1 day ago time in PHP microtime() format.
I am trying like this. But still showing the current time.
$date = date("Y-m-d H:i:s", strtotime('-1 day'));
$val = microtime($date);
http://us2.php.net/manual/en/function.microtime.php
microtime() returns the current Unix timestamp with microseconds
You can't use microtime() to get a time from the past, since the funtion only works with current date.
Wouldn't this work for you?
$date = strtotime('-1 day');
Edit: Code corrected according to u_mulder's comment.

Converting Date Format for Calculations in PHP

I'm displaying a due date for tasks in an m/d/y format. I'm displaying the day the task was posted in a "Posted X $name(s) ago" (eg. "Posted 6 day(s) ago").
I'm working on giving the timestamps (posted and due date) different CSS classes, depending on how many days there are from TODAY until the due date. (So the "Posted X" timestamp is less relevant, I just wanted to give a better picture.)
So far, I have the following down:
$cdate = $this->data['due'];
$today = time();
$dovi = date('m/d/Y', $today);
$difference = $cdate - $dovi;
$upcoming= floor($difference/60/60/24);
$cdate is pulling the due date from the DB, in m/d/y format. $today is telling us what today is (using the UNIX timestamp). $dovi is converting time() in to the m/d/y format. $difference is telling us the difference between today and the due date. $upcoming, in theory, should take that difference and dumb it down into a simple number.
I think that for the calculation to work, I would need to convert $cdate in to a UNIX timestamp or somehow convert both $today and $cdate into some other matching format other than m/d/y.
Does anyone have suggestions on the best way to make this work? I already have some code to run the CSS changes, the only thing I'm stuck on is this conversion/calculation issue to determine how many days from NOW (time()) the due date is. Thanks!!
Maybe something like this? Assuming $this->data['due'] is in m/d/Y format.
$this->data['due'] = '7/28/2012';
$diff = strtotime($this->data['due']) - strtotime(date('m/d/Y'));
var_dump(date('d',$diff)); // 3
You can pass any valid parameter to php's date function to have it formatted however you would like.
Its long one, but works...
$datetime = new DateTime("2012-07-22 02:03:50"); // your date in datetime type
$curr_stamp = time();
$act_stamp = mktime($datetime->format('H'), $datetime->format('i'), $datetime->format('s'), $datetime->format('n'), $datetime->format('j'), $datetime->format('Y'));
$diff=$curr_stamp-$act_stamp;
$day_diff = floor($diff / 86400);
if($day_diff < 7)
echo $day_diff." days ago";
Rather elegant and right solution (and also viable after the end of UNIX epoch).
$today = new DateTime(); // creating `today` DateTime object
$expiry = DateTime::createFromFormat('m/d/Y', $this->data['due']) // creating DateTime object from already formatted date
$difference = $today->diff($expiry); // 1st variant to calculate difference between dates
$difference = date_diff($today, $expiry); // and 2nd variant
echo $difference->format('Interval (difference) is %R% days');
Remember that UNIX epoch (timestamp) will "end" "soon" and code based on timestamps possibly will face some problems (maybe we will find the solution in future to avoid this, but ...), it is better to use DateTime class, bec. even to calculate number of years for those who are born before 1970 year can become kinda problem if you don't remember the date 1970.01.01 and trying to do it using timestamps (it is widespread database practice BTW :) ).
Never do it (timestamps) for very old dates and look to the future and DateTime will SaveOurSouls.

Easiest way to get the unix time stamp for the date exactly 1 month back in php

I want to get the unix time stamp for that day exactly 30days back from current day.
What is the best method?
Can i use this to get the date 30 days back, is this the best method?
$day = date('Y-m-d', strtotime('-30 days'));
a google search brings me to mktime() function in php. But how do i combine both and get the unix time stamp for the day? What is the easiest and fastest method?
You just need to use the strtotime("-1 month"); function. That will return a UNIX timestamp.
$date = date_create();
date_sub($date, date_interval_create_from_date_string('1 m'));
echo date_format($date, 'U');
How to get previous month and year relative to today, using strtotime and date? watch this question. there is the discussion about the funny strtotime("-1 month"); bug.

Compare timestamp to date

I need to compare a timestamp to a date. I would just like to compare the date portion without the time bit. I need to check whether a timestamp occurs on the day before yesterday i.e. today - 2.
Could you show me a snippet please? Thank you.
I've been reading through the PHP docs but couldn't find a very clean way of doing this. What I found was converting the timestamp to a date with a particular format and comparing it to a date which I get by doing a time delta to get the date before yesterday and converting it to a particular format. Messy.
You can arcieve this by using the function strtotime.
To round to a day I personaly like to edit the timestamp. This is a notations of seconds since epoch. One day is 86400 seconds, so if you do the following caculation:
$time = $time - ( $time % 86400 );
You can convert it back to a date again with the date function of PHP, for example:
$readableFormat = date( 'd-m-Y', $time );
There is also much on the internet about this topic.
you can use the strtotime function
<?php
$time = strtotime("5 june 2010");
$before = strtotime("-1 day",$time);
$after = strtotime("+1 day",$time);

Categories